/
graphics.go
72 lines (66 loc) · 1.18 KB
/
graphics.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
/**
*Copyright (c) 2018 Stephen Merrony
*
*This software is released under the MIT License.
*https://opensource.org/licenses/MIT
*/
// graphics drawing primitives
package main
import (
"image"
"image/color"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
)
func drawPhysLabel(rgba *image.RGBA, x, y int, lab string, col color.Color) {
point := fixed.Point26_6{
X: fixed.Int26_6(x * 64),
Y: fixed.Int26_6(y * 64)}
d := &font.Drawer{
Dst: rgba,
Src: image.NewUniform(col),
Face: basicfont.Face7x13,
Dot: point,
}
d.DrawString(lab)
}
// drawPhysLine draws a straight line using Bresenham's algorithm on an image.RGBA
// using physical coordinates (origin is top-left)
func drawPhysLine(rgba *image.RGBA, x0, y0, x1, y1 int, col color.Color) {
dx := x1 - x0
if dx < 0 {
dx = -dx
}
dy := y1 - y0
if dy < 0 {
dy = -dy
}
var sx, sy int
if x0 < x1 {
sx = 1
} else {
sx = -1
}
if y0 < y1 {
sy = 1
} else {
sy = -1
}
err := dx - dy
for {
rgba.Set(x0, y0, col)
if x0 == x1 && y0 == y1 {
break
}
e2 := 2 * err
if e2 > -dy {
err -= dy
x0 += sx
}
if e2 < dx {
err += dx
y0 += sy
}
}
}