// Package position is a simple 2D position library. // Positions consist of a vector.Vector and a rotation. package position import ( "math" "git.wtrh.nl/wouter/gopatterns/pkg/vector" ) // Position contains a 2D vector and a rotation angle. type Position struct { Vector vector.Vector Rotation float64 } // Add a position. func (p Position) Add(q Position) Position { return Position{ Vector: p.Vector.Add(q.Vector.Rotate(p.Rotation)), Rotation: p.Rotation + q.Rotation, } } // Translate the position to a new reference frame. func (p Position) Translate(q Position) Position { q.Vector = q.Vector.Span(q.Rotation) return p.Add(q) } // Distance returns the distance between two positions. func (p Position) Distance(q Position) float64 { return p.Vector.Distance(q.Vector) } // Direction returns the angle to another position. func (p Position) Direction(q Position) float64 { return math.Remainder(p.Vector.AngleBetween(q.Vector)-p.Rotation, 2*math.Pi) }