-
Notifications
You must be signed in to change notification settings - Fork 7
/
clampLen.go
120 lines (110 loc) · 3.53 KB
/
clampLen.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package govec
// V2F
// ClampLen returns a new V2F[T] with the length of vector clamped to min and max.
// When the length of the vector is outside the range min and max, the vector is scaled to fit.
func (v V2F[T]) ClampLen(min float64, max float64) V2F[T] {
length := v.Len()
if length == 0 {
return V2F[T]{X: 0, Y: 0}
}
if length < min {
scale := min / length
return V2F[T]{X: v.X * T(scale), Y: v.Y * T(scale)}
} else if length > max {
scale := max / length
return V2F[T]{X: v.X * T(scale), Y: v.Y * T(scale)}
}
return v
}
// ClampLenInPlace modifies vector by clamping the length of the vector to min and max.
// When the length of the vector is outside the range min and max, the vector is scaled to fit.
func (v *V2F[T]) ClampLenInPlace(min float64, max float64) {
length := v.Len()
if length == 0 {
v.X = 0
v.Y = 0
}
if length < min {
scale := min / length
v.X *= T(scale)
v.Y *= T(scale)
} else if length > max {
scale := max / length
v.X *= T(scale)
v.Y *= T(scale)
}
}
// V3F
// ClampLen returns a new V3F[T] with the length of vector clamped to min and max.
// When the length of the vector is outside the range min and max, the vector is scaled to fit.
func (v V3F[T]) ClampLen(min float64, max float64) V3F[T] {
length := v.Len()
if length == 0 {
return V3F[T]{X: 0, Y: 0, Z: 0}
}
if length < min {
scale := min / length
return V3F[T]{X: v.X * T(scale), Y: v.Y * T(scale), Z: v.Z * T(scale)}
} else if length > max {
scale := max / length
return V3F[T]{X: v.X * T(scale), Y: v.Y * T(scale), Z: v.Z * T(scale)}
}
return v
}
// ClampLenInPlace modifies vector by clamping the length of the vector to min and max.
// When the length of the vector is outside the range min and max, the vector is scaled to fit.
func (v *V3F[T]) ClampLenInPlace(min float64, max float64) {
length := v.Len()
if length == 0 {
v.X = 0
v.Y = 0
v.Z = 0
}
if length < min {
scale := min / length
v.X *= T(scale)
v.Y *= T(scale)
v.Z *= T(scale)
} else if length > max {
scale := max / length
v.X *= T(scale)
v.Y *= T(scale)
v.Z *= T(scale)
}
}
// V2I
// ClampLen returns a new V2F[T] with the length of vector clamped to min and max.
// This converts the vector to a V2F[float64] as the integer vector cannot be scaled.
// When the length of the vector is outside the range min and max, the vector is scaled to fit.
func (v V2I[T]) ClampLen(min float64, max float64) V2F[float64] {
length := v.Len()
if length == 0 {
return V2F[float64]{X: 0, Y: 0}
}
if length < min {
scale := min / length
return V2F[float64]{X: float64(v.X) * scale, Y: float64(v.Y) * scale}
} else if length > max {
scale := max / length
return V2F[float64]{X: float64(v.X) * scale, Y: float64(v.Y) * scale}
}
return V2F[float64]{X: float64(v.X), Y: float64(v.Y)}
}
// V3I
// ClampLen returns a new V3F[T] with the length of vector clamped to min and max.
// This converts the vector to a V3F[float64] as the integer vector cannot be scaled.
// When the length of the vector is outside the range min and max, the vector is scaled to fit.
func (v V3I[T]) ClampLen(min float64, max float64) V3F[float64] {
length := v.Len()
if length == 0 {
return V3F[float64]{X: 0, Y: 0, Z: 0}
}
if length < min {
scale := min / length
return V3F[float64]{X: float64(v.X) * scale, Y: float64(v.Y) * scale, Z: float64(v.Z) * scale}
} else if length > max {
scale := max / length
return V3F[float64]{X: float64(v.X) * scale, Y: float64(v.Y) * scale, Z: float64(v.Z) * scale}
}
return V3F[float64]{X: float64(v.X), Y: float64(v.Y), Z: float64(v.Z)}
}