You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 line
897B

  1. // Package position is a simple 2D position library.
  2. // Positions consist of a vector.Vector and a rotation.
  3. package position
  4. import (
  5. "math"
  6. "git.wtrh.nl/patterns/gopatterns/pkg/vector"
  7. )
  8. // Position contains a 2D vector and a rotation angle.
  9. type Position struct {
  10. Vector vector.Vector
  11. Rotation float64
  12. }
  13. // Add a position.
  14. func (p Position) Add(q Position) Position {
  15. return Position{
  16. Vector: p.Vector.Add(q.Vector.Rotate(p.Rotation)),
  17. Rotation: p.Rotation + q.Rotation,
  18. }
  19. }
  20. func (p Position) RotationD() float64 {
  21. return p.Rotation*180/math.Pi
  22. }
  23. // Distance returns the distance between two positions.
  24. func (p Position) Distance(q Position) float64 {
  25. return p.Vector.Distance(q.Vector)
  26. }
  27. // Direction returns the angle to another position.
  28. func (p Position) Direction(q Position) float64 {
  29. return math.Remainder(p.Vector.AngleBetween(q.Vector)-p.Rotation, 2*math.Pi)
  30. }