-
Notifications
You must be signed in to change notification settings - Fork 0
/
floatRange.go
50 lines (42 loc) · 1.05 KB
/
floatRange.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package common
import (
"fmt"
"math"
)
// Represents a range of values between min and max inclusive
type FloatRange struct {
Min, Max float32
}
//NewFloatRange creates a new FloatRange with the given min and max values.
func NewFloatRange(min, max float32) FloatRange {
if max < min {
return FloatRange{
Min: max,
Max: min,
}
}
return FloatRange{
Min: min,
Max: max,
}
}
// Gets the length of the range
func (r *FloatRange) GetLength() float32 {
return float32(math.Abs(float64(r.Max - r.Min)))
}
// Checks if specific value falls inside the range
func (r *FloatRange) IsValueInside(value float32) bool {
return value >= r.Min && value <= r.Max
}
// IsRangeInside checks if the given range is inside this range.
func (r *FloatRange) IsRangeInside(r2 FloatRange) bool {
return r.IsValueInside(r2.Min) && r.IsValueInside(r2.Max)
}
//Move shifts the range's min and max values by the given value.
func (r *FloatRange) Move(d float32) {
r.Min += d
r.Max += d
}
func (r *FloatRange) ToString() string {
return fmt.Sprintf("[%f %f]", r.Min, r.Max)
}