-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.go
52 lines (44 loc) · 932 Bytes
/
vec.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
package main
type Vec [3]float64
func (res *Vec) Add(a, b *Vec) *Vec {
(*res)[0] = (*a)[0] + (*b)[0]
(*res)[1] = (*a)[1] + (*b)[1]
(*res)[2] = (*a)[2] + (*b)[2]
return res
}
func (res *Vec) Sub(a, b *Vec) *Vec {
(*res)[0] = (*a)[0] - (*b)[0]
(*res)[1] = (*a)[1] - (*b)[1]
(*res)[2] = (*a)[2] - (*b)[2]
return res
}
func (a *Vec) Equals(b *Vec) bool {
for i := range *a {
if (*a)[i] != (*b)[i] {
return false
}
}
return true
}
func (res *Vec) Clamp(s *Vec) {
for i := range *res {
if (*res)[i] > (*s)[i]/2 {
(*res)[i] = (*s)[i] / 2
}
if (*res)[i] < -(*s)[i]/2 {
(*res)[i] = -(*s)[i] / 2
}
}
}
func Dot(a, b *Vec) float64 {
return (*a)[0]*(*b)[0] + (*a)[1]*(*b)[1] + (*a)[2]*(*b)[2]
}
func (v *Vec) Nrm2Sq() float64 {
return Dot(v, v)
}
func (res *Vec) Scale(alpha float64, v *Vec) *Vec {
(*res)[0] = alpha * (*v)[0]
(*res)[1] = alpha * (*v)[1]
(*res)[2] = alpha * (*v)[2]
return res
}