-
Notifications
You must be signed in to change notification settings - Fork 7
/
cross.go
73 lines (63 loc) · 1.76 KB
/
cross.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
package govec
// V3F
// Cross returns a new V3F[T] with the cross product of given vectors.
func (v V3F[T]) Cross(v2 V3F[T]) V3F[T] {
return V3F[T]{
X: v.Y*v2.Z - v.Z*v2.Y,
Y: v.Z*v2.X - v.X*v2.Z,
Z: v.X*v2.Y - v.Y*v2.X,
}
}
// CrossInPlace modifies vector by setting it to the cross product of given vectors.
func (v *V3F[T]) CrossInPlace(v2 V3F[T]) {
x := v.Y*v2.Z - v.Z*v2.Y
y := v.Z*v2.X - v.X*v2.Z
z := v.X*v2.Y - v.Y*v2.X
v.X, v.Y, v.Z = x, y, z
}
// CrossComp returns a new V3F[T] with the cross product of given vectors.
func (v V3F[T]) CrossComp(x T, y T, z T) V3F[T] {
return V3F[T]{
X: v.Y*z - v.Z*y,
Y: v.Z*x - v.X*z,
Z: v.X*y - v.Y*x,
}
}
// CrossCompInPlace modifies vector by setting it to the cross product of given vectors.
func (v *V3F[T]) CrossCompInPlace(x T, y T, z T) {
nx := v.Y*z - v.Z*y
ny := v.Z*x - v.X*z
nz := v.X*y - v.Y*x
v.X, v.Y, v.Z = nx, ny, nz
}
// V3I
// Cross returns a new V3I[T] with the cross product of given vectors.
func (v V3I[T]) Cross(v2 V3I[T]) V3I[T] {
return V3I[T]{
X: v.Y*v2.Z - v.Z*v2.Y,
Y: v.Z*v2.X - v.X*v2.Z,
Z: v.X*v2.Y - v.Y*v2.X,
}
}
// CrossInPlace modifies vector by setting it to the cross product of given vectors.
func (v *V3I[T]) CrossInPlace(v2 V3I[T]) {
x := v.Y*v2.Z - v.Z*v2.Y
y := v.Z*v2.X - v.X*v2.Z
z := v.X*v2.Y - v.Y*v2.X
v.X, v.Y, v.Z = x, y, z
}
// CrossComp returns a new V3I[T] with the cross product of given vectors.
func (v V3I[T]) CrossComp(x T, y T, z T) V3I[T] {
return V3I[T]{
X: v.Y*z - v.Z*y,
Y: v.Z*x - v.X*z,
Z: v.X*y - v.Y*x,
}
}
// CrossCompInPlace modifies vector by setting it to the cross product of given vectors.
func (v *V3I[T]) CrossCompInPlace(x T, y T, z T) {
nx := v.Y*z - v.Z*y
ny := v.Z*x - v.X*z
nz := v.X*y - v.Y*x
v.X, v.Y, v.Z = nx, ny, nz
}