-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.go
76 lines (67 loc) · 1.56 KB
/
math.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
package main
import "math"
// CrossProduct calculates the cross product of two vectors
func CrossProduct(a, b []float64) []float64 {
if len(a) < 3 || len(b) < 3 {
panic("invalid vector length")
}
cross := make([]float64, 3)
cross[0] = a[1]*b[2] - a[2]*b[1]
cross[1] = a[2]*b[0] - a[0]*b[2]
cross[2] = a[0]*b[1] - a[1]*b[0]
return cross
}
func Normal(p0, p1, p2 []float64) []float64 {
return []float64{
(p1[1]-p0[1])*(p2[2]-p0[2]) - (p1[2]-p0[2])*(p2[1]-p0[1]),
(p1[2]-p0[2])*(p2[0]-p0[0]) - (p1[0]-p0[0])*(p2[2]-p0[2]),
(p1[0]-p0[0])*(p2[1]-p0[1]) - (p1[1]-p0[1])*(p2[0]-p0[0]),
}
}
// DotProduct calculates the dot product of two vectors
func DotProduct(a, b []float64) float64 {
if len(a) != len(b) {
panic("vector lengths are unequal")
}
sum := 0.0
for i := range a {
sum += a[i] * b[i]
}
return sum
}
func Magnitude(a []float64) float64 {
magnitude := 0.0
for i := range a {
magnitude += a[i] * a[i]
}
return math.Sqrt(magnitude)
}
func Normalize(a []float64) []float64 {
magnitude := Magnitude(a)
normalized := make([]float64, len(a))
for i := range a {
normalized[i] = a[i] / magnitude
}
return normalized
}
func Add(a, b []float64) []float64 {
sum := make([]float64, len(a))
for i := range a {
sum[i] = a[i] + b[i]
}
return sum
}
func Subtract(a, b []float64) []float64 {
difference := make([]float64, len(a))
for i := range a {
difference[i] = a[i] - b[i]
}
return difference
}
func Scale(a []float64, factor float64) []float64 {
scaled := make([]float64, len(a))
for i := range a {
scaled[i] = a[i] * factor
}
return scaled
}