Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

34 Zeilen
823B

  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. // Distance returns the distance between two positions.
  21. func (p Position) Distance(q Position) float64 {
  22. return p.Vector.Distance(q.Vector)
  23. }
  24. // Direction returns the angle to another position.
  25. func (p Position) Direction(q Position) float64 {
  26. return math.Remainder(p.Vector.AngleBetween(q.Vector)-p.Rotation, 2*math.Pi)
  27. }