-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
36 lines (32 loc) · 864 Bytes
/
util.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
package main
import (
"math"
)
// Clamp ensures that x is in the range [a, b]. It returns x if x is in [a,
// b], a if x < a, and b if x > b. Clamp does NOT check that a < b, and the
// behavior is undefined in this case.
func Clamp(a, x, b float64) float64 {
return math.Max(a, math.Min(x, b))
}
// Unclamp ensures that x is outside the range (a, b). It returns x if x is
// outside (a, b), and either a or b, whichever is closest to x, if x is inside
// (a, b). Unclamp does NOT check that a < b, and the behavior is undefined in
// this case.
func Unclamp(a, x, b float64) float64 {
if x <= a || x >= b {
return x
}
if x >= (a+b)/2 {
return b
}
return a
}
// SliceContains returns true if the slice contains x.
func SliceContains[T comparable](slice []T, x T) bool {
for _, y := range slice {
if x == y {
return true
}
}
return false
}