Clamp values falling outside of the axis bounds.

This commit is contained in:
Anna Rose Wiggins 2025-07-10 19:58:31 -04:00
parent 681e1fef70
commit a6ad1b609a
3 changed files with 17 additions and 6 deletions

View file

@ -4,17 +4,22 @@ import (
"golang.org/x/exp/constraints"
)
func AbsInt[T constraints.Integer](value T) T {
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 ClampInt[T constraints.Integer](value, min, max T) T {
func Clamp[T Numeric](value, min, max T) T {
if value < min {
value = min
}