-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.go
More file actions
70 lines (60 loc) · 1.22 KB
/
vector.go
File metadata and controls
70 lines (60 loc) · 1.22 KB
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
package nvector
import (
"math"
)
// Vector is a 3D vector.
type Vector struct {
X, Y, Z float64
}
// Add returns v+w.
func (v Vector) Add(w Vector) Vector {
return Vector{
v.X + w.X,
v.Y + w.Y,
v.Z + w.Z,
}
}
// Cross returns the cross product of v and w.
func (v Vector) Cross(w Vector) Vector {
return Vector{
v.Y*w.Z - v.Z*w.Y,
v.Z*w.X - v.X*w.Z,
v.X*w.Y - v.Y*w.X,
}
}
// Dot returns the dot product of v and w.
func (v Vector) Dot(w Vector) float64 {
return v.X*w.X + v.Y*w.Y + v.Z*w.Z
}
// Norm returns the Euclidean norm of v.
func (v Vector) Norm() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y + v.Z*v.Z)
}
// Normalize returns a vector in the same direction as v but with norm 1.
func (v Vector) Normalize() Vector {
return v.Scale(1 / v.Norm())
}
// Scale returns v scaled by s.
func (v Vector) Scale(s float64) Vector {
return Vector{
v.X * s,
v.Y * s,
v.Z * s,
}
}
// Sub returns v-w.
func (v Vector) Sub(w Vector) Vector {
return Vector{
v.X - w.X,
v.Y - w.Y,
v.Z - w.Z,
}
}
// Transform returns v transformed by m.
func (v Vector) Transform(m Matrix) Vector {
return Vector{
m.XX*v.X + m.XY*v.Y + m.XZ*v.Z,
m.YX*v.X + m.YY*v.Y + m.YZ*v.Z,
m.ZX*v.X + m.ZY*v.Y + m.ZZ*v.Z,
}
}