-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathrender.go
164 lines (155 loc) · 3.57 KB
/
render.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package rush
import (
"fmt"
"image"
"math"
"strings"
"github.com/fogleman/gg"
)
const (
showBlockedSquares = false
showPieceLabels = false
showSolution = false
)
const (
cellSize = 160
padding = 32
labelFontSize = 36
footerFontSize = 24
)
const (
backgroundColor = "FFFFFF"
boardColor = "F2EACD"
blockedColor = "D96D60"
gridLineColor = "222222"
primaryPieceColor = "CC3333"
pieceColor = "338899"
pieceOutlineColor = "222222"
labelColor = "222222"
wallColor = "222222"
)
const labelFont = "/Library/Fonts/Arial.ttf"
const footerFont = "/System/Library/Fonts/Menlo.ttc"
func renderBoard(board *Board) image.Image {
const S = cellSize
bw := board.Width
bh := board.Height
w := bw * S
h := bh * S
iw := w + padding*2
ih := h + padding*2
if showSolution {
ih += 120
}
dc := gg.NewContext(iw, ih)
dc.LoadFontFace(labelFont, labelFontSize)
dc.Translate(padding, padding)
dc.SetHexColor(backgroundColor)
dc.Clear()
dc.SetHexColor(boardColor)
dc.DrawRectangle(0, 0, float64(w+1), float64(h+1))
dc.Fill()
if showBlockedSquares {
for _, i := range board.BlockedSquares() {
x := float64(i % bw)
y := float64(i / bw)
dc.DrawRectangle(x*S, y*S, S, S)
}
dc.SetHexColor(blockedColor)
dc.Fill()
}
ex := float64(bw) * S
ey := float64(board.Pieces[0].Row(bw))*S + S/2
es := float64(S) / 10
dc.LineTo(ex, ey+es)
dc.LineTo(ex, ey-es)
dc.LineTo(ex+es, ey)
dc.SetHexColor(gridLineColor)
dc.Fill()
p := S / 8.0
r := S / 32.0
for _, i := range board.Walls {
x := float64(i % bw)
y := float64(i / bw)
dc.DrawRectangle(x*S, y*S, S, S)
dc.SetHexColor(wallColor)
dc.Fill()
dc.DrawCircle(x*S+p, y*S+p, r)
dc.DrawCircle(x*S+S-p, y*S+p, r)
dc.DrawCircle(x*S+p, y*S+S-p, r)
dc.DrawCircle(x*S+S-p, y*S+S-p, r)
dc.SetHexColor(boardColor)
dc.Fill()
}
for x := S; x < w; x += S {
fx := float64(x)
dc.DrawLine(fx, 0, fx, float64(h))
}
for y := S; y < h; y += S {
fy := float64(y)
dc.DrawLine(0, fy, float64(w), fy)
}
dc.SetHexColor(gridLineColor)
dc.SetLineWidth(2)
dc.Stroke()
dc.DrawRectangle(0, 0, float64(w+1), float64(h+1))
dc.SetLineWidth(6)
dc.Stroke()
for i, piece := range board.Pieces {
stride := 1
if piece.Orientation == Vertical {
stride = bw
}
i0 := piece.Position
i1 := i0 + stride*(piece.Size-1)
x0 := float64(i0 % bw)
y0 := float64(i0 / bw)
x1 := float64(i1 % bw)
y1 := float64(i1 / bw)
dx := x1 - x0
dy := y1 - y0
m := S / 8.0
px := x0*S + m
py := y0*S + m
pw := dx*S + S - m*2
ph := dy*S + S - m*2
dc.DrawRoundedRectangle(px+0.5, py+0.5, pw, ph, S/8.0)
if i == 0 {
dc.SetHexColor(primaryPieceColor)
} else {
dc.SetHexColor(pieceColor)
}
dc.FillPreserve()
dc.SetLineWidth(S / 32.0)
dc.SetHexColor(pieceOutlineColor)
dc.Stroke()
if showPieceLabels {
tx := px + pw/2
ty := py + ph/2
dc.SetHexColor(labelColor)
dc.DrawStringAnchored(string('A'+i), tx, ty, 0.5, 0.5)
}
}
if showSolution {
x := float64(w) / 2
y := float64(h) + padding*0.75
footer := ""
solution := board.Solve()
if solution.Solvable {
moveStrings := make([]string, len(solution.Moves))
for i, move := range solution.Moves {
moveStrings[i] = move.String()
}
footer = fmt.Sprintf("%s (%d moves)",
strings.Join(moveStrings, " "), solution.NumMoves)
}
dc.LoadFontFace(footerFont, footerFontSize)
var tw float64
for _, line := range dc.WordWrap(footer, float64(w)) {
w, _ := dc.MeasureString(line)
tw = math.Max(tw, w)
}
dc.DrawStringWrapped(footer, x, y, 0.5, 0, tw, 1.5, gg.AlignLeft)
}
return dc.Image()
}