-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawCircle.go
More file actions
74 lines (60 loc) · 1.6 KB
/
DrawCircle.go
File metadata and controls
74 lines (60 loc) · 1.6 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
package gographics
import (
"image"
)
// https://iq.opengenus.org/bresenhams-circle-drawing-algorithm/
func DrawCircle(img *image.Paletted, vec [2]int, radius int, col uint8) {
x := 0
y := radius
d := 3 - 2*radius
displayBresenhamCirlce(img, vec[0], vec[1], x, y, col)
for y >= x {
x++
if d > 0 {
y--
d = d + 4*(x-y) + 10
} else {
d = d + 4*x + 6
}
displayBresenhamCirlce(img, vec[0], vec[1], x, y, col)
}
}
// https://stackoverflow.com/questions/1201200/fast-algorithm-for-drawing-filled-circles
func DrawFilledCirlce(img *image.Paletted, vec [2]int, radius int, col uint8) {
x := 0
y := radius
d := 3 - 2*radius
displayBresenhamCirlce(img, vec[0], vec[1], x, y, col)
fillBresenhamCircle(img, vec[0], vec[1], x, y, col)
for y >= x {
x++
if d > 0 {
y--
d = d + 4*(x-y) + 10
} else {
d = d + 4*x + 6
}
displayBresenhamCirlce(img, vec[0], vec[1], x, y, col)
fillBresenhamCircle(img, vec[0], vec[1], x, y, col)
}
}
func displayBresenhamCirlce(img *image.Paletted, xc, yc, x, y int, col uint8) {
img.SetColorIndex(xc+x, yc+y, col)
img.SetColorIndex(xc-x, yc+y, col)
img.SetColorIndex(xc+x, yc-y, col)
img.SetColorIndex(xc-x, yc-y, col)
img.SetColorIndex(xc+y, yc+x, col)
img.SetColorIndex(xc-y, yc+x, col)
img.SetColorIndex(xc+y, yc-x, col)
img.SetColorIndex(xc-y, yc-x, col)
}
func fillBresenhamCircle(img *image.Paletted, xc, yc, x, y int, col uint8) {
for i := xc - x; i <= xc+x; i++ {
img.SetColorIndex(i, yc+y, col)
img.SetColorIndex(i, yc-y, col)
}
for i := xc - y; i <= xc+y; i++ {
img.SetColorIndex(i, yc+x, col)
img.SetColorIndex(i, yc-x, col)
}
}