-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.go
145 lines (105 loc) · 2.5 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package hexagen
import (
"fmt"
"image/color"
"math"
)
func adjacent(a, b FaceAddr) bool {
if a.Orientation == b.Orientation {
return false
}
if a.X == b.X && a.Y == b.Y {
return true
}
if a.X == b.X {
return int(math.Abs(float64(a.Y-b.Y))) == 1
}
if a.Y == b.Y {
return int(math.Abs(float64(a.X-b.X))) == 1
}
return false
}
type mat struct {
a, b, c, d float64
}
func (m mat) rmul(v vec) vec {
return vec{m.a*v.x + m.b*v.y, m.c*v.x + m.d*v.y}
}
type vec struct {
x, y float64
}
func (v vec) add(w vec) vec {
return vec{v.x + w.x, v.y + w.y}
}
func (v vec) String() string {
return fmt.Sprintf("{% 1.1f, % 1.1f}", v.x, v.y)
}
func resolve(x, y float64) FaceAddr {
v := vec{x * 4, y * 4}
m := mat{1, -1 / math.Sqrt(3), 0, 2 / math.Sqrt(3)}
w := m.rmul(v)
w.x += 1
// in welchem dreieck?
o := Orientation(w.x-math.Floor(w.x)+w.y-math.Floor(w.y) < 1)
addr := FaceAddr{X: int(math.Floor(w.x)), Y: int(math.Floor(w.y)), Orientation: o}
return addr
}
func inhexagon(addr FaceAddr) bool {
acc := true
acc = acc && (addr.X > 0 || addr.Y > 0) // exclude (0, 0)
acc = acc && (addr.X >= 0)
acc = acc && (addr.Y >= 0)
acc = acc && !(addr.X == 1 && addr.Y == 0 && addr.Orientation == Up)
acc = acc && !(addr.X == 0 && addr.Y == 1 && addr.Orientation == Up)
acc = acc && (addr.X < 4)
acc = acc && (addr.Y < 4)
acc = acc && !(addr.X == 3 && addr.Y == 2 && addr.Orientation == Down)
acc = acc && !(addr.X == 2 && addr.Y == 3 && addr.Orientation == Down)
acc = acc && (addr.X < 3 || addr.Y < 3) // exclude (3, 3)
return acc
}
// Generate takes some data and a (pixel) width and generates a Grid that can be encoded into an image.
func Generate(data []byte, width float64) *Grid {
var g Grid
g.m = make(map[FaceAddr]color.CMYK, 0)
g.w = width
cur := FaceAddr{2, 0, true}
prev := FaceAddr{2, -1, false}
delta := cur.Sub(prev)
for _, b := range data {
for j := 0; j < 2; j++ {
//fmt.Println(cur)
input := (b & 1) > 0
b >>= 1
n := next(cur, cur.Sub(delta), input)
prev = cur
cur = n
delta = cur.Sub(prev)
cur = wrap(cur)
col := g.m[cur]
//col.A = 0xff
col.C += b & 1
b >>= 1
col.M += b & 1
b >>= 1
col.Y += b & 1
b >>= 1
g.m[cur] = col
}
}
var max float64
for _, col := range g.m {
max = math.Max(
math.Max(float64(col.C), float64(col.M)),
math.Max(float64(col.Y), float64(max)),
)
}
scale := byte(0xff / max)
for addr, col := range g.m {
col.C *= scale
col.M *= scale
col.Y *= scale
g.m[addr] = col
}
return &g
}