forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.go
163 lines (136 loc) · 2.38 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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package math
import (
"math"
)
func R2D(r float32) float32 {
return 180.0 * r / Pi
}
func D2R(r float32) float32 {
return Pi * r / 180.0
}
func Absf(v float32) float32 {
if v < 0 {
return -v
} else {
return v
}
}
func Round(v float32) int {
if v < 0 {
return int(v - 0.5)
} else {
return int(v + 0.4999999)
}
}
func Sinf(v float32) float32 {
return float32(math.Sin(float64(v)))
}
func Cosf(v float32) float32 {
return float32(math.Cos(float64(v)))
}
func Tanf(v float32) float32 {
return float32(math.Tan(float64(v)))
}
func Asinf(v float32) float32 {
return float32(math.Asin(float64(v)))
}
func Acosf(v float32) float32 {
return float32(math.Acos(float64(v)))
}
func Atanf(v float32) float32 {
return float32(math.Atan(float64(v)))
}
func Sqrtf(v float32) float32 {
return float32(math.Sqrt(float64(v)))
}
func Powf(v, e float32) float32 {
return float32(math.Pow(float64(v), float64(e)))
}
func Lerp(a, b int, s float32) int {
r := float32(b - a)
return a + int(r*s)
}
func Lerpf(a, b float32, s float32) float32 {
r := b - a
return a + r*s
}
func Ramp(s float32, a, b float32) float32 {
return (s - a) / (b - a)
}
func RampSat(s float32, a, b float32) float32 {
return Saturate((s - a) / (b - a))
}
func Saturate(x float32) float32 {
return Clampf(x, 0, 1)
}
func SmoothStep(s float32, a, b float32) float32 {
x := RampSat(s, a, b)
return x * x * (3 - 2*x)
}
func Clamp(x, min, max int) int {
switch {
case x < min:
return min
case x > max:
return max
default:
return x
}
}
func Clampf(x, min, max float32) float32 {
switch {
case x < min:
return min
case x > max:
return max
default:
return x
}
}
func Min(values ...int) int {
m := MaxInt
for _, v := range values {
if v < m {
m = v
}
}
return m
}
func Minf(values ...float32) float32 {
m := float32(math.MaxFloat32)
for _, v := range values {
if v < m {
m = v
}
}
return m
}
func Max(values ...int) int {
m := MinInt
for _, v := range values {
if v > m {
m = v
}
}
return m
}
func Maxf(values ...float32) float32 {
m := float32(-math.MaxFloat32)
for _, v := range values {
if v > m {
m = v
}
}
return m
}
func Mod(a, b int) int {
x := a % b
if x < 0 {
return x + b
} else {
return x
}
}