forked from tanema/ump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.go
77 lines (62 loc) · 1.18 KB
/
math.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
package ump
import (
"math"
"math/rand"
)
var (
inf = float32(math.Inf(1))
delta float32 = 1e-10
)
func clamp(x, minX, maxX float32) float32 {
if x < minX {
return minX
} else if x > maxX {
return maxX
}
return x
}
func randMax(max float32) float32 {
return randRange(0, max)
}
func randRange(min, max float32) float32 {
return (rand.Float32() * (max - min)) + min
}
func randLimits(limit float32) float32 {
return randRange(-limit, limit)
}
func abs(x float32) float32 {
return float32(math.Abs(float64(x)))
}
func min(x, y float32) float32 {
return float32(math.Min(float64(x), float64(y)))
}
func max(x, y float32) float32 {
return float32(math.Max(float64(x), float64(y)))
}
func sin(x float32) float32 {
return float32(math.Sin(float64(x)))
}
func cos(x float32) float32 {
return float32(math.Cos(float64(x)))
}
func sign(x float32) float32 {
if x > 0 {
return 1
}
if x == 0 {
return 0
}
return -1
}
func crossProduct(x1, y1, x2, y2 float32) float32 {
return x1*y2 - y1*x2
}
func nearest(x, a, b float32) float32 {
if abs(a-x) < abs(b-x) {
return a
}
return b
}
func frac(n float32) float32 {
return abs(n - float32(math.Floor(float64(n))))
}