-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnorm.go
52 lines (41 loc) · 1.43 KB
/
norm.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
package govec
import (
"math"
)
// V2F
// Norm returns a new vector with the same direction and magnitude 1.
func (v V2F[T]) Norm() V2F[T] {
magnitude := math.Sqrt(float64(v.X*v.X + v.Y*v.Y))
return V2F[T]{X: v.X / T(magnitude), Y: v.Y / T(magnitude)}
}
// NormInPlace modifies the vector by normalizing it.
func (v *V2F[T]) NormInPlace() {
magnitude := math.Sqrt(float64(v.X*v.X + v.Y*v.Y))
v.X /= T(magnitude)
v.Y /= T(magnitude)
}
// V3F
// Norm returns a new vector with the same direction and magnitude 1.
func (v V3F[T]) Norm() V3F[T] {
magnitude := math.Sqrt(float64(v.X*v.X + v.Y*v.Y + v.Z*v.Z))
return V3F[T]{X: v.X / T(magnitude), Y: v.Y / T(magnitude), Z: v.Z / T(magnitude)}
}
// NormInPlace modifies the vector by normalizing it.
func (v *V3F[T]) NormInPlace() {
magnitude := math.Sqrt(float64(v.X*v.X + v.Y*v.Y + v.Z*v.Z))
v.X /= T(magnitude)
v.Y /= T(magnitude)
v.Z /= T(magnitude)
}
// V2I
// Norm returns a new float vector with the same direction and magnitude 1.
func (v V2I[T]) Norm() V2F[float64] {
magnitude := math.Sqrt(float64(v.X*v.X + v.Y*v.Y))
return V2F[float64]{X: float64(v.X) / magnitude, Y: float64(v.Y) / magnitude}
}
// V3I
// Norm returns a new float vector with the same direction and magnitude 1.
func (v V3I[T]) Norm() V3F[float64] {
magnitude := math.Sqrt(float64(v.X*v.X + v.Y*v.Y + v.Z*v.Z))
return V3F[float64]{X: float64(v.X) / magnitude, Y: float64(v.Y) / magnitude, Z: float64(v.Z) / magnitude}
}