forked from fogleman/simplify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.go
49 lines (39 loc) · 884 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
package simplify
import "math"
type Vector struct {
X, Y, Z float64
}
func (a Vector) Less(b Vector) bool {
if a.X != b.X {
return a.X < b.X
}
if a.Y != b.Y {
return a.Y < b.Y
}
return a.Z < b.Z
}
func (a Vector) Length() float64 {
return math.Sqrt(a.X*a.X + a.Y*a.Y + a.Z*a.Z)
}
func (a Vector) Dot(b Vector) float64 {
return a.X*b.X + a.Y*b.Y + a.Z*b.Z
}
func (a Vector) Cross(b Vector) Vector {
x := a.Y*b.Z - a.Z*b.Y
y := a.Z*b.X - a.X*b.Z
z := a.X*b.Y - a.Y*b.X
return Vector{x, y, z}
}
func (a Vector) Normalize() Vector {
d := a.Length()
return Vector{a.X / d, a.Y / d, a.Z / d}
}
func (a Vector) Add(b Vector) Vector {
return Vector{a.X + b.X, a.Y + b.Y, a.Z + b.Z}
}
func (a Vector) Sub(b Vector) Vector {
return Vector{a.X - b.X, a.Y - b.Y, a.Z - b.Z}
}
func (a Vector) MulScalar(b float64) Vector {
return Vector{a.X * b, a.Y * b, a.Z * b}
}