-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector.go
63 lines (48 loc) · 1.44 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
package main
import (
"math"
)
type Vector struct {
X, Y, Z float64
}
func (this Vector) add(that Vector) Vector {
return Vector{this.X + that.X, this.Y + that.Y, this.Z + that.Z}
}
func (this Vector) subtract(that Vector) Vector {
return Vector{this.X - that.X, this.Y - that.Y, this.Z - that.Z}
}
func (this Vector) multiply(that Vector) Vector {
return Vector{this.X * that.X, this.Y * that.Y, this.Z * that.Z}
}
func (this Vector) divide(that Vector) Vector {
return Vector{this.X / that.X, this.Y / that.Y, this.Z / that.Z}
}
func (this Vector) scale(that float64) Vector {
return Vector{this.X * that, this.Y * that, this.Z * that}
}
func (this Vector) divideFloat(that float64) Vector {
return Vector{this.X / that, this.Y / that, this.Z / that}
}
func (this Vector) multiplyFold(that Vector) float64 {
t := this.multiply(that)
return t.X + t.Y + t.Z
}
func (this Vector) dot(that Vector) float64 {
return this.X * that.X + this.Y * that.Y + this.Z * that.Z
}
func (this Vector) lengthSquared() float64 {
return this.multiplyFold(this);
}
func(this Vector) length() float64 {
return math.Sqrt(this.lengthSquared())
}
func(this Vector) norm() Vector {
return this.divideFloat(this.length())
}
func(this Vector) unitVector() Vector {
return this.scale(1 / this.length())
}
func(this Vector) reflectThrough(normal Vector) Vector {
d := normal.scale(this.dot(normal))
return d.scale(2).subtract(this)
}