]> git.friedersdorff.com Git - max/vector.go.git/blob - vector.go
Add GPLv3 License to module
[max/vector.go.git] / vector.go
1 // vector provides a Vec type and methods for performing vector arithmetic
2 package vector
3
4 // 3 dimensional vector
5 type Vec struct {
6   X, Y, Z  float64
7 }
8
9 // Cross returns the cross product between vectors v and o
10 func (v Vec) Cross(o Vec) Vec {
11   return Vec{v.Y*o.Z - v.Z*o.Y, v.Z*o.X - v.X*o.Z, v.X*o.Y - v.Y*o.X}
12 }
13
14 // Doc returns the dot product between vectors v and o
15 func (v Vec) Dot(o Vec) float64 {
16   return v.X*o.X + v.Y*o.Y + v.Z*o.Z
17 }
18
19 // sub subtracts o from v
20 func (v Vec) Sub(o Vec) Vec {
21   return Vec{v.X - o.X, v.Y - o.Y, v.Z - o.Z}
22 }