-
Notifications
You must be signed in to change notification settings - Fork 12
/
types.go
122 lines (105 loc) · 2.23 KB
/
types.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package numerical
import (
"math"
"math/rand"
)
// A Vec3 is a 3-dimensional tuple of floats.
type Vec3 [3]float64
// NewVec3RandomNormal creates a random normal vector.
func NewVec3RandomNormal() Vec3 {
return Vec3{
rand.NormFloat64(),
rand.NormFloat64(),
rand.NormFloat64(),
}
}
// Add returns v + v1.
func (v Vec3) Add(v1 Vec3) Vec3 {
return Vec3{v[0] + v1[0], v[1] + v1[1], v[2] + v1[2]}
}
// Sub returns v - v1.
func (v Vec3) Sub(v1 Vec3) Vec3 {
return Vec3{v[0] - v1[0], v[1] - v1[1], v[2] - v1[2]}
}
// Scale returns v * f.
func (v Vec3) Scale(f float64) Vec3 {
return Vec3{v[0] * f, v[1] * f, v[2] * f}
}
// Dist gets the Euclidean distance ||v - v1||.
func (v Vec3) Dist(v1 Vec3) float64 {
dx := v[0] - v1[0]
dy := v[1] - v1[1]
dz := v[2] - v1[2]
return math.Sqrt(dx*dx + dy*dy + dz*dz)
}
// Sum gets the sum of the components.
func (v Vec3) Sum() float64 {
return v[0] + v[1] + v[2]
}
// Vec is a vector of arbitrary dimension.
type Vec []float64
// Add returns v + v1.
func (v Vec) Add(v1 Vec) Vec {
if len(v) != len(v1) {
panic("vector lengths do not match")
}
res := make(Vec, len(v))
for i, x := range v {
y := v1[i]
res[i] = x + y
}
return res
}
// Sub returns v - v1.
func (v Vec) Sub(v1 Vec) Vec {
if len(v) != len(v1) {
panic("vector lengths do not match")
}
res := make(Vec, len(v))
for i, x := range v {
y := v1[i]
res[i] = x - y
}
return res
}
// Dot returns the dot product of v and v1.
func (v Vec) Dot(v1 Vec) float64 {
if len(v) != len(v1) {
panic("vector lengths do not match")
}
var res float64
for i, x := range v {
y := v1[i]
res += x * y
}
return res
}
// Scale returns v * s.
func (v Vec) Scale(s float64) Vec {
res := make(Vec, len(v))
for i, x := range v {
res[i] = x * s
}
return res
}
// NormSquared returns ||v||^2.
func (v Vec) NormSquared() float64 {
var res float64
for _, x := range v {
res += x * x
}
return res
}
// Norm returns ||v||.
func (v Vec) Norm() float64 {
return math.Sqrt(v.NormSquared())
}
// Normalize returns v/||v||.
func (v Vec) Normalize() Vec {
return v.Scale(1 / v.Norm())
}
// ProjectOut projects the direction v1 out of v.
func (v Vec) ProjectOut(v1 Vec) Vec {
normed := v1.Normalize()
return v.Sub(normed.Scale(normed.Dot(v)))
}