-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.go
68 lines (53 loc) · 1.34 KB
/
vector.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
package pewpewpew
import (
"fmt"
"math"
)
type Vector struct {
X, Y, Z float64
}
func (v Vector) String() string {
return fmt.Sprintf("X: %v Y: %v Z: %v", v.X, v.Y, v.Z)
}
func (v Vector) Equals(v2 Vector) bool {
return v.X == v2.X && v.Y == v2.Y && v.Z == v2.Z
}
func (v Vector) Add(v2 Vector) Vector {
return Vector{v.X + v2.X, v.Y + v2.Y, v.Z + v2.Z}
}
func (v Vector) Sub(v2 Vector) Vector {
return Vector{v.X - v2.X, v.Y - v2.Y, v.Z - v2.Z}
}
func (v Vector) Mult(m float64) Vector {
return Vector{v.X * m, v.Y * m, v.Z * m}
}
func (v Vector) Product(v2 Vector) Vector {
return Vector{v.X * v2.X, v.Y * v2.Y, v.Z * v2.Z}
}
func (v Vector) Div(m float64) Vector {
return Vector{v.X / m, v.Y / m, v.Z / m}
}
func (v Vector) Invert() Vector {
return Vector{v.X * -1, v.Y * -1, v.Z * -1}
}
// Get the pYthagorean length (or magnitude) of the vector.
func (v Vector) Length() float64 {
return math.Sqrt((v.X * v.X) + (v.Y * v.Y) + (v.Z * v.Z))
}
func (v Vector) SquaredLength() float64 {
return (v.X * v.X) + (v.Y * v.Y) + (v.Z * v.Z)
}
func (v Vector) Unit() Vector {
l := v.Length()
return Vector{v.X / l, v.Y / l, v.Z / l}
}
func Dot(v, v2 Vector) float64 {
return v.X*v2.X + v.Y*v2.Y + v.Z*v2.Z
}
func Cross(v, v2 Vector) Vector {
return Vector{
X: v.Y*v2.Z - v.Z*v2.Y,
Y: -(v.X*v2.Z - v.Z*v2.X),
Z: v.X*v2.Y - v.Y*v2.X,
}
}