Add more maths functions for the Vectors
This commit is contained in:
parent
14977de5bc
commit
e81ceecffc
2 changed files with 195 additions and 0 deletions
|
@ -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()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue