-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
72 lines (55 loc) · 1.23 KB
/
types.go
File metadata and controls
72 lines (55 loc) · 1.23 KB
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
package hexagen
import (
"fmt"
"image"
"image/color"
"math"
)
type Orientation bool
func (o Orientation) String() string {
if o == Up {
return "Up"
}
return "Down"
}
const (
Down Orientation = false
Up = true
)
type FaceAddr struct {
X, Y int
Orientation Orientation
}
func (a FaceAddr) Sub(b FaceAddr) FaceAddr {
return FaceAddr{X: a.X - b.X, Y: a.Y - b.Y, Orientation: a.Orientation != b.Orientation}
}
func (addr FaceAddr) String() string {
return fmt.Sprintf("{% 1d, % 1d, %v}", addr.X, addr.Y, addr.Orientation)
}
type Grid struct {
m map[FaceAddr]color.CMYK
w float64
}
func (g Grid) At(x, y int) color.Color {
height := g.w * math.Sqrt(3) / 2
padding := int(g.w-height) / 2
y -= padding
if y < 0 || y > int(float64(g.w)*math.Sqrt(3)/2) {
return color.Transparent
}
y = int(float64(g.w)/2*math.Sqrt(3)) - y
addr := resolve(float64(x)/g.w, float64(y)/g.w)
if !inhexagon(addr) {
return color.Transparent
}
if col, ok := g.m[addr]; ok {
return col
}
return color.White
}
func (g Grid) ColorModel() color.Model {
return color.NRGBAModel
}
func (g Grid) Bounds() image.Rectangle {
return image.Rectangle{Min: image.Point{X: 0, Y: 0}, Max: image.Point{X: int(g.w), Y: int(g.w)}}
}