@@ -2,6 +2,42 @@ package mathutils
22
33import "math"
44
5+ const (
6+ degToRad = math .Pi / 180.0
7+ radToDeg = 180.0 / math .Pi
8+ )
9+
10+ // Radians converts an angle measured in degrees to its value in radians.
11+ func Radians (degrees float64 ) float64 {
12+ return degrees * degToRad
13+ }
14+
15+ // Degrees converts an angle measured in radians to its value in degrees.
16+ func Degrees (radians float64 ) float64 {
17+ return radians * radToDeg
18+ }
19+
20+ // MapRange maps a value v from one range [a, b] to another range [c, d].
21+ func MapRange (v , a , b , c , d float64 ) float64 {
22+ return (v - a )/ (b - a )* (d - c ) + c
23+ }
24+
25+ // Fract returns the fractional part of x.
26+ func Fract (x float64 ) float64 {
27+ return x - math .Floor (x )
28+ }
29+
30+ // Clamp returns value clamped to [low, high]
31+ func Clamp (value , low , high float64 ) float64 {
32+ if value < low {
33+ return low
34+ }
35+ if value > high {
36+ return high
37+ }
38+ return value
39+ }
40+
541// LinSpace returns a slice of float64 values spaced evenly between min and max.
642//
743// If n is less than or equal to 1, it returns a slice with only the min value.
@@ -31,13 +67,3 @@ func SinSpace(amplitude float64, n int) []float64 {
3167 }
3268 return tValues
3369}
34-
35- // MapRange maps a value v from one range [a, b] to another range [c, d].
36- func MapRange (v , a , b , c , d float64 ) float64 {
37- return (v - a )/ (b - a )* (d - c ) + c
38- }
39-
40- // Fract returns the fractional part of x.
41- func Fract (x float64 ) float64 {
42- return x - math .Floor (x )
43- }
0 commit comments