Add more maths functions for the Vectors

This commit is contained in:
Marc Di Luzio 2020-06-05 15:48:44 +01:00
parent 14977de5bc
commit e81ceecffc
2 changed files with 195 additions and 0 deletions

View file

@ -1,5 +1,7 @@
package game
import "math"
// Vector desribes a 3D vector
type Vector struct {
X float64 `json:"x"`
@ -11,3 +13,25 @@ func (v *Vector) Add(v2 Vector) {
v.X += v2.X
v.Y += v2.Y
}
// Added calculates a new vector
func (v Vector) Added(v2 Vector) Vector {
v.Add(v2)
return v
}
// Negated returns a negated vector
func (v Vector) Negated() Vector {
return Vector{-v.X, -v.Y}
}
// Length returns the length of the vector
func (v Vector) Length() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
// Distance returns the distance between two vectors
func (v Vector) Distance(v2 Vector) float64 {
// Negate the two vectors and calciate the length
return v.Added(v2.Negated()).Length()
}