-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.go
50 lines (38 loc) · 968 Bytes
/
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
// Inspired by Physically Based Rendering, 3rd edition
package main
import "math"
type Vector3 struct {
X, Y, Z float64
}
func (a Vector3) Add(b Vector3) Vector3 {
return Vector3{a.X + b.X, a.Y + b.Y, a.Z + b.Z}
}
func (a Vector3) Sub(b Vector3) Vector3 {
return Vector3{a.X - b.X, a.Y - b.Y, a.Z - b.Z}
}
func (v Vector3) Multiply(f float64) Vector3 {
return Vector3{v.X * f, v.Y * f, v.Z * f}
}
func (v Vector3) Divide(f float64) Vector3 {
inv := 1 / f
return v.Multiply(inv)
}
func (v Vector3) LengthSquared() float64 {
return v.X*v.X + v.Y*v.Y + v.Z*v.Z
}
func (v Vector3) Length() float64 {
return math.Sqrt(v.LengthSquared())
}
func (v Vector3) Normalize() Vector3 {
return v.Divide(v.Length())
}
func (v Vector3) Dot(v2 Vector3) float64 {
return v.X*v2.X + v.Y*v2.Y + v.Z*v2.Z
}
func (v Vector3) Cross(v2 Vector3) Vector3 {
return Vector3{
(v.Y * v2.Z) - (v.Z * v2.Y),
(v.Z * v2.X) - (v.X * v2.Z),
(v.X * v2.Y) - (v.Y * v2.X),
}
}