/
geometry.go
63 lines (51 loc) · 1.06 KB
/
geometry.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
package main
import (
"image/draw"
"image"
"image/color"
"math"
)
/* Line drawing - Bresenhams algorithm in it's simplest form.*/
func DrawLine(screen draw.Image, a image.Point, b image.Point, color color.Color) {
dx := b.X-a.X
dy := b.Y-a.Y
error := 0.0
derr := math.Abs(float64(dy)/float64(dx))
y := a.Y
for x := a.X ; x < b.X ; x++ {
screen.Set(x, y, color)
error += derr
if error >= 0.5 {
y++
error -= 1.0
}
}
}
func DrawCircle(screen draw.Image, c image.Point, r int, color color.Color) {
f := 1-r
ddF_x := 1
ddF_y := -2*r
x,y := 0,r
screen.Set(c.X, c.Y+r, color)
screen.Set(c.X, c.Y-r, color)
screen.Set(c.X+r, c.Y, color)
screen.Set(c.X-r, c.Y, color)
for ; x < y ; {
if f >= 0 {
y--
ddF_y += 2
f += ddF_y
}
x++
ddF_x += 2
f += ddF_x
screen.Set(c.X+x, c.Y+y, color)
screen.Set(c.X-x, c.Y+y, color)
screen.Set(c.X+x, c.Y-y, color)
screen.Set(c.X-x, c.Y-y, color)
screen.Set(c.X+y, c.Y+x, color)
screen.Set(c.X-y, c.Y+x, color)
screen.Set(c.X+y, c.Y-x, color)
screen.Set(c.X-y, c.Y-x, color)
}
}