-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdraw.go
More file actions
79 lines (72 loc) · 1.66 KB
/
draw.go
File metadata and controls
79 lines (72 loc) · 1.66 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
73
74
75
76
77
78
79
package ilda
import (
"image"
"image/color"
"image/draw"
)
// A Point is an X, Y coordinate pair. The axes increase right and down.
//
// X: Extreme left: 0, extreme right max.X
// Y: Extreme top: 0, extreme bottom max.Y
//
// ILDA Point
//
// X: Extreme left: -32768, extreme right: +32767
// Y: Extreme bottom: -32768, extreme top: +32767
//
// [-32768, +32767] -> [min.X, min.Y] (0, 0)
// [+32767, -32768] -> [max.X, max.Y] (65535, 65535)
func (p Point) Point(r image.Rectangle) image.Point {
pp := image.Pt(int(p.X), -int(p.Y)).Add(image.Pt(0x8000, 0x8000))
pp.X, pp.Y = pp.X*r.Dx(), pp.Y*r.Dy()
return pp.Div(0x10000).Add(image.Pt(1, -1))
}
// Draw aligns r.Min in dst with sp in src and then replaces the
// rectangle r in dst with the result of drawing src on dst.
func (f *Frame) Draw(dst draw.Image, r image.Rectangle, src image.Image, sp image.Point) {
// copy background
draw.Draw(dst, r, src, sp, draw.Src)
var plt plot
for _, pt := range f.Points {
switch pt.Color {
case color.Transparent:
plt.move(pt.Point(r))
default:
plt.draw(dst, pt.Point(r), pt.Color)
}
}
}
type plot image.Point
func (p0 *plot) move(p1 image.Point) {
p0.X, p0.Y = p1.X, p1.Y
}
func abs(x int) (int, int) {
if x < 0 {
return -x, -1
}
return x, 1
}
func (p0 *plot) draw(img draw.Image, p1 image.Point, c color.Color) {
dx, sx := abs(p1.X - p0.X)
dy, sy := abs(p1.Y - p0.Y)
dy = -dy
err := dx + dy // error value e_xy
for {
img.Set(p0.X, p0.Y, c)
e2 := 2 * err
if e2 >= dy { // e_xy + e_x > 0
if p0.X == p1.X {
break
}
err += dy
p0.X += sx
}
if e2 <= dx { // e_xy + e_y < 0
if p0.Y == p1.Y {
break
}
err += dx
p0.Y += sy
}
}
}