2020-06-02 18:00:27 +01:00
|
|
|
package game
|
|
|
|
|
2020-06-05 15:48:44 +01:00
|
|
|
import "math"
|
|
|
|
|
2020-06-03 18:12:08 +01:00
|
|
|
// Vector desribes a 3D vector
|
|
|
|
type Vector struct {
|
|
|
|
X float64 `json:"x"`
|
|
|
|
Y float64 `json:"y"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds one vector to another
|
|
|
|
func (v *Vector) Add(v2 Vector) {
|
|
|
|
v.X += v2.X
|
|
|
|
v.Y += v2.Y
|
2020-06-02 18:00:27 +01:00
|
|
|
}
|
2020-06-05 15:48:44 +01:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|