joyful/internal/mappingrules/math.go
Anna Rose Wiggins 8d2b15a7c8 Move initialization code closer to the appropriate structs. (#17)
Reviewed-on: #17
Co-authored-by: Anna Rose Wiggins <annabunches@gmail.com>
Co-committed-by: Anna Rose Wiggins <annabunches@gmail.com>
2025-08-12 00:57:11 +00:00

43 lines
752 B
Go

package mappingrules
import (
"golang.org/x/exp/constraints"
)
type Numeric interface {
constraints.Integer | constraints.Float
}
func Abs[T Numeric](value T) T {
return max(value, -value)
}
// LerpInt linearly interpolates between two integer values using
// a float64 index value
func LerpInt[T constraints.Integer](min, max T, t float64) T {
t = Clamp(t, 0.0, 1.0)
return T((1-t)*float64(min) + t*float64(max))
}
func Clamp[T Numeric](value, min, max T) T {
if value < min {
value = min
}
if value > max {
value = max
}
return value
}
func clampAndShift(start, end, min, max int32) (int32, int32) {
if start < min {
end += min - start
start = min
}
if end > max {
start -= end - max
end = max
}
return start, end
}