Skip to content

Commit cf2b251

Browse files
committed
new methods
1 parent 5028655 commit cf2b251

2 files changed

Lines changed: 37 additions & 11 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/setanarut/mathutils
22

3-
go 1.24.3
3+
go 1.24.4

mathutils.go

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,42 @@ package mathutils
22

33
import "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

Comments
 (0)