This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 809
/
math.go
174 lines (134 loc) · 3.4 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package d2math
import "math"
const (
// Epsilon is used as the threshold for 'almost equal' operations.
Epsilon float64 = 0.0001
// RadToDeg is used to convert anges in radians to degrees by multiplying the radians by RadToDeg. Similarly,degrees
// are converted to radians when dividing by RadToDeg.
RadToDeg float64 = 57.29578
// RadFull is the radian equivalent of 360 degrees.
RadFull float64 = 6.283185253783088
)
// EqualsApprox returns true if the difference between a and b is less than Epsilon.
func EqualsApprox(a, b float64) bool {
return Abs(a-b) < Epsilon
}
// CompareApprox returns an integer between -1 and 1 describing the comparison of floats a and b. 0 will be returned if
// the absolute difference between a and b is less than Epsilon.
func CompareApprox(a, b float64) int {
delta := a - b
if Abs(delta) < Epsilon {
return 0
}
if delta > 0 {
return 1
}
return -1
}
// Abs returns the absolute value of a. It is a less CPU intensive version of the standard library math.Abs().
func Abs(a float64) float64 {
if a < 0 {
return -a
}
return a
}
// Clamp returns a clamped to min and max.
func Clamp(a, min, max float64) float64 {
if a > max {
return max
} else if a < min {
return min
}
return a
}
// Sign returns the sign of a.
func Sign(a float64) int {
switch {
case a < 0:
return -1
case a > 0:
return +1
}
return 0
}
// Lerp returns the linear interpolation from a to b using interpolator x.
func Lerp(a, b, x float64) float64 {
return a + x*(b-a)
}
// Unlerp returns the intepolator Lerp would require to return x when given
// a and b. The x argument of this function can be thought of as the return
// value of lerp. The return value of this function can be used as x in
// Lerp.
func Unlerp(a, b, x float64) float64 {
return (x - a) / (b - a)
}
// WrapInt wraps x to between 0 and max. For example WrapInt(450, 360) would return 90.
func WrapInt(x, max int) int {
wrapped := x % max
if wrapped < 0 {
return max + wrapped
}
return wrapped
}
// MinInt returns the minimum of the given values
func MinInt(a, b int) int {
if a < b {
return a
}
return b
}
// MaxInt returns the maximum of the given values
func MaxInt(a, b int) int {
if a > b {
return a
}
return b
}
// Min returns the lower of two values
func Min(a, b uint32) uint32 {
if a < b {
return a
}
return b
}
// Max returns the higher of two values
func Max(a, b uint32) uint32 {
if a > b {
return a
}
return b
}
// MaxInt32 returns the higher of two values
func MaxInt32(a, b int32) int32 {
if a > b {
return a
}
return b
}
// AbsInt32 returns the absolute of the given int32
func AbsInt32(a int32) int32 {
if a < 0 {
return -a
}
return a
}
// MinInt32 returns the higher of two values
func MinInt32(a, b int32) int32 {
if a < b {
return a
}
return b
}
// BytesToInt32 converts 4 bytes to int32
// IsoToScreen converts isometric coordinates to screenspace coordinates
// ScreenToIso converts screenspace coordinates to isometric coordinates
// GetRadiansBetween returns the radians between two points. 0rad is facing to the right.
func GetRadiansBetween(p1X, p1Y, p2X, p2Y float64) float64 {
deltaY := p2Y - p1Y
deltaX := p2X - p1X
return math.Atan2(deltaY, deltaX)
}
// ClampInt ensures that the given value is between or equal to the given min or max
func ClampInt(value, min, max int) int {
return int(math.Min(math.Max(float64(value), float64(min)), float64(max)))
}