-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_shapes.go
76 lines (70 loc) · 2.21 KB
/
draw_shapes.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
package main
import (
"image"
"image/color"
)
func drawRectangleOutline(img *image.RGBA, rect image.Rectangle, outlineColor color.RGBA, width int) {
if drawUI && !playOnlyMode {
// Draw top edge
drawLine(img, rect.Min.X, rect.Min.Y, rect.Max.X, rect.Min.Y, outlineColor, width)
// Draw bottom edge
drawLine(img, rect.Min.X, rect.Max.Y, rect.Max.X, rect.Max.Y, outlineColor, width)
// Draw left edge
drawLine(img, rect.Min.X, rect.Min.Y, rect.Min.X, rect.Max.Y, outlineColor, width)
// Draw right edge
drawLine(img, rect.Max.X, rect.Min.Y, rect.Max.X, rect.Max.Y, outlineColor, width)
}
}
func drawLine(img *image.RGBA, x0, y0, x1, y1 int, color color.RGBA, width int) {
if drawUI && !playOnlyMode {
// Bresenham's line algorithm to draw the line
dx := abs(x1 - x0)
dy := -abs(y1 - y0)
sx := 1
sy := 1
if x0 >= x1 {
sx = -1
}
if y0 >= y1 {
sy = -1
}
err := dx + dy
for {
// Set the color of the pixel at the specified coordinates
for i := x0 - width/2; i <= x0+width/2; i++ {
for j := y0 - width/2; j <= y0+width/2; j++ {
img.SetRGBA(i, j, color)
}
}
if x0 == x1 && y0 == y1 {
break
}
e2 := 2 * err
if e2 >= dy {
err += dy
x0 += sx
}
if e2 <= dx {
err += dx
y0 += sy
}
}
}
}
func drawPoint(img *image.RGBA, p image.Point, color color.RGBA, radius int) {
if drawUI && !playOnlyMode {
x, y := p.X, p.Y
for i := x - radius; i <= x+radius; i++ {
for j := y - radius; j <= y+radius; j++ {
// Calculate the distance from the center
dx := x - i
dy := y - j
distance := dx*dx + dy*dy
// If the distance is less than or equal to the radius squared, set the color
if distance <= radius*radius {
img.SetRGBA(i, j, color)
}
}
}
}
}