選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

39 行
969B

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