-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
97 lines (81 loc) · 2.41 KB
/
utils.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
package main
import (
"log"
"math"
"math/rand/v2"
"github.com/adrg/sysfont"
rl "github.com/gen2brain/raylib-go/raylib"
"golang.org/x/exp/constraints"
)
func getSystemFontPath() string {
finder := sysfont.NewFinder(&sysfont.FinderOpts{
Extensions: []string{".ttf"},
})
font := finder.Match("Segoe UI")
if font == nil {
log.Fatal("system font not found")
}
return font.Filename
}
// [min, max)
func randRange(min, max int) int {
return rand.IntN(max-min) + min
}
func clamp(min, max, val int) int {
if val > max {
return max
} else if val < min {
return min
}
return val
}
// snapNumber takes a float64 and returns a function that snaps a value to the nearest multiple of the input
func snapNumber[T constraints.Float](point T) func(T) T {
return func(v T) T {
return T(math.Round(float64(v/point))) * point
}
}
// snapResult is a struct to hold the result of the SnapSlice function
type snapResult[T constraints.Float] struct {
point T
index int
}
// snapSlice takes a slice of T and returns a function that snaps a value to the nearest point in the slice
func snapSlice[T constraints.Float](points []T) func(T) snapResult[T] {
return func(v T) snapResult[T] {
if len(points) == 0 {
return snapResult[T]{point: 0, index: -1}
}
lastDistance := T(math.Abs(float64(points[0] - v)))
result := snapResult[T]{point: points[0], index: 0}
for i := 1; i < len(points); i++ {
distance := T(math.Abs(float64(points[i] - v)))
if distance == 0 {
return snapResult[T]{point: points[i], index: i}
}
if distance > lastDistance {
return result
}
result = snapResult[T]{point: points[i], index: i}
lastDistance = distance
}
return result // return the last item as the result
}
}
func mapRange[T constraints.Float](value, fromLow, fromHigh, toLow, toHigh T) T {
return (value-fromLow)*(toHigh-toLow)/(fromHigh-fromLow) + toLow
}
func drawLinesAroundCircle(center rl.Vector2, radius float32, lineCount int, lineLength float32, color rl.Color) {
for i := 0; i < lineCount; i++ {
angle := float32(i) / float32(lineCount) * 2 * math.Pi
start := rl.Vector2{
X: center.X + float32(math.Cos(float64(angle)))*radius,
Y: center.Y + float32(math.Sin(float64(angle)))*radius,
}
end := rl.Vector2{
X: center.X + float32(math.Cos(float64(angle)))*(radius+lineLength),
Y: center.Y + float32(math.Sin(float64(angle)))*(radius+lineLength),
}
rl.DrawLineV(start, end, color)
}
}