-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.go
96 lines (77 loc) · 2.39 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
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
// Copyright 2017, the project authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.md file.
package graphics
import "math"
// Represents a vector in 3d space
type Vector struct {
X, Y, Z float64
}
// Shorthand to create a new vector with the given components
func NewVec(x, y, z float64) Vector {
return Vector{x, y, z}
}
// Calculates the length of this vector
func (a Vector) Length() float64 {
return math.Sqrt(a.X*a.X + a.Y*a.Y + a.Z*a.Z)
}
// Calculates the square length of this vector
func (a Vector) LengthSquared() float64 {
return a.X*a.X + a.Y*a.Y + a.Z*a.Z
}
// Calculates the dot product of this vector and the given vector
func (a Vector) Dot(b Vector) float64 {
return a.X*b.X + a.Y*b.Y + a.Z*b.Z
}
// Calculates the cross product of this vector and the given vector
func (a Vector) Cross(b Vector) Vector {
x := a.Y*b.Z - a.Z*b.Y
y := a.Z*b.X - a.X*b.Z
z := a.X*b.Y - a.Y*b.X
return NewVec(x, y, z)
}
// Normalizes the vector
func (a Vector) Normalize() Vector {
d := a.Length()
return NewVec(a.X/d, a.Y/d, a.Z/d)
}
// Negates the vector
func (a Vector) Negate() Vector {
return NewVec(-a.X, -a.Y, -a.Z)
}
// Calculates the absolute value of the vector piece-wise
func (a Vector) Abs() Vector {
return NewVec(math.Abs(a.X), math.Abs(a.Y), math.Abs(a.Z))
}
// Adds the vector piece-wise to the given vector
func (a Vector) Add(b Vector) Vector {
return NewVec(a.X+b.X, a.Y+b.Y, a.Z+b.Z)
}
// Subtracts the vector piece-wise from the given vector
func (a Vector) Sub(b Vector) Vector {
return NewVec(a.X-b.X, a.Y-b.Y, a.Z-b.Z)
}
// Multiplies the vector piece-wise by the given vector
func (a Vector) Mul(b Vector) Vector {
return NewVec(a.X*b.X, a.Y*b.Y, a.Z*b.Z)
}
// Divides the vector piece-wise by the given vector
func (a Vector) Div(b Vector) Vector {
return NewVec(a.X/b.X, a.Y/b.Y, a.Z/b.Z)
}
// Adds the given scalar amount to the vector
func (a Vector) AddS(b float64) Vector {
return NewVec(a.X+b, a.Y+b, a.Z+b)
}
// Subtracts the given scalar amount from the vector
func (a Vector) SubS(b float64) Vector {
return NewVec(a.X-b, a.Y-b, a.Z-b)
}
// Multiplies the vector by the given scalar amount
func (a Vector) MulS(b float64) Vector {
return NewVec(a.X*b, a.Y*b, a.Z*b)
}
// Divides the vector by the given scalar amount
func (a Vector) DivS(b float64) Vector {
return NewVec(a.X/b, a.Y/b, a.Z/b)
}