-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
128 lines (116 loc) · 2.1 KB
/
util.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"math/rand"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
type Point struct {
X, Y int
}
func LinePoints(x0, y0, x1, y1 int) []Point {
var points []Point
// implemented straight from WP pseudocode
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 {
points = append(points, Point{X: x0, Y: y0})
if x0 == x1 && y0 == y1 {
break
}
e2 := 2 * err
if e2 > -dy {
err -= dy
x0 += sx
}
if e2 < dx {
err += dx
y0 += sy
}
}
return points
}
func CirclePoints(x, y, r int) []Point {
var points []Point
if r < 0 {
return nil
}
// Bresenham algorithm
x1, y1, err := -r, 0, 2-2*r
for {
points = append(points,
Point{X: x - x1, Y: y + y1},
Point{X: x - y1, Y: y - x1},
Point{X: x + x1, Y: y - y1},
Point{X: x + y1, Y: y + x1},
)
r = err
if r > x1 {
x1++
err += x1*2 + 1
}
if r <= y1 {
y1++
err += y1*2 + 1
}
if x1 >= 0 {
break
}
}
return points
}
func CircleThickPoints(x, y, r int) (points []Point) {
circle := CirclePoints(x, y, r)
for _, cp := range circle {
points = append(points, cp)
points = append(points, CirclePoints(cp.X, cp.Y, 1)...)
}
return
}
func RectPoints(x1, y1, x2, y2 int) (points []Point) {
for x := x1; x <= x2; x++ {
points = append(points, Point{X: x, Y: y1})
points = append(points, Point{X: x, Y: y2})
}
for y := y1; y <= y2; y++ {
points = append(points, Point{X: x1, Y: y})
points = append(points, Point{X: x2, Y: y})
}
return
}
// repeatingKeyPressed return true when key is pressed considering the repeat state.
func repeatingKeyPressed(key ebiten.Key) bool {
const (
delay = 30
interval = 3
)
d := inpututil.KeyPressDuration(key)
if d == 1 {
return true
}
if d >= delay && (d-delay)%interval == 0 {
return true
}
return false
}
// RandIntRange returns a random int in the range [min, max].
func RandIntRange(min, max int) int {
return rand.Intn(max+1-min) + min
}