-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.go
166 lines (148 loc) · 2.89 KB
/
world.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
165
166
package turtle
import (
"image"
"image/draw"
"image/png"
"os"
)
// A world to draw on.
type World struct {
Image *image.RGBA
Width, Height int
DrawLineCh chan Line
closeCh chan bool
}
// Create a new empty World.
func NewWorld(width, height int) *World {
img := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(img, img.Bounds(), &image.Uniform{SoftBlack}, image.Point{0, 0}, draw.Src)
return NewWorldImage(img)
}
// Create a new World attached to the Image img,
// and start listening on w.DrawLineCh for lines to draw.
func NewWorldImage(img *image.RGBA) *World {
drawCh := make(chan Line)
closeCh := make(chan bool)
w := &World{
Image: img,
Width: img.Bounds().Max.X,
Height: img.Bounds().Max.Y,
DrawLineCh: drawCh,
closeCh: closeCh,
}
go w.listen()
return w
}
// Save output
func (w *World) SaveImage(filePath string) error {
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
err = png.Encode(f, w.Image)
return err
}
// Close the world channels, and stop the listen goroutine.
func (w *World) Close() {
w.closeCh <- true
}
// listen for draw commands on drawLineCh.
func (w *World) listen() {
for {
select {
// draw the received line
case line := <-w.DrawLineCh:
w.drawLine(line)
w.DrawLineCh <- line
// close the channels and exit the func
case <-w.closeCh:
close(w.closeCh)
close(w.DrawLineCh)
return
}
}
}
// Draw a line on the image.
func (w *World) drawLine(l Line) {
x0 := int(l.X0)
y0 := int(l.Y0)
x1 := int(l.X1)
y1 := int(l.Y1)
// line is vertical
if x0 == x1 {
if y0 > y1 {
y1, y0 = y0, y1
}
for i := y0; i <= y1; i++ {
w.setPoint(x0, i, l.p)
}
return
}
// line is horizontal
if y0 == y1 {
if x0 > x1 {
x1, x0 = x0, x1
}
for i := x0; i <= x1; i++ {
w.setPoint(i, y0, l.p)
}
return
}
// line is diagonal, draw it with Bresenham algo
dx := intAbs(x1 - x0)
dy := -intAbs(y1 - y0)
var sx, sy int
if x0 < x1 {
sx = 1
} else {
sx = -1
}
if y0 < y1 {
sy = 1
} else {
sy = -1
}
err := dx + dy
var e2 int
for {
w.setPoint(x0, y0, l.p)
if x0 == x1 && y0 == y1 {
return
}
e2 = 2 * err
if e2 >= dy {
err += dy
x0 += sx
}
if e2 <= dx {
err += dx
y0 += sy
}
}
}
// Draw a point on the image.
func (w *World) setPoint(x, y int, p *Pen) {
// the y in the reference frame of the image
yr := w.Height - y - 1
// always draw at least one pixel
if p.Size <= 1 {
w.Image.Set(x, yr, p.Color)
return
}
half := p.Size / 2
before := half
// if the size is even, remove a pixel from the left/bottom
// in the cartesian coord
if p.Size%2 == 0 {
before = half - 1
}
// fill the square
for i := -before; i <= half; i++ {
for ii := -before; ii <= half; ii++ {
// yr-ii because before/half are in cartesian coord
// so we move to image coord by flipping the y axis
w.Image.Set(x+i, yr-ii, p.Color)
}
}
}