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.

93 Zeilen
2.3KB

  1. package point
  2. import (
  3. "math"
  4. "git.wtrh.nl/wouter/gopatterns/pkg/position"
  5. "git.wtrh.nl/wouter/gopatterns/pkg/vector"
  6. "github.com/tdewolff/canvas"
  7. )
  8. // BetweenPoint defines a point on the line between two other points.
  9. type BetweenPoint struct {
  10. id ID
  11. p Point
  12. q Point
  13. offset float64
  14. name string
  15. draw bool
  16. hide bool
  17. }
  18. // NewBetweenPoint returns a new BetweenPoint relative to two other points p and q.
  19. // The given offset defines where the new point is.
  20. // With offset = 0 the new point is a p, offset = 0.5 results in a point exactly in the middle.
  21. // Offset can be <0 (extending from p side) or >1 (extending from the q side).
  22. func NewBetweenPoint(p, q Point, offset float64, id ID) *BetweenPoint {
  23. return &BetweenPoint{
  24. id: id,
  25. p: p,
  26. q: q,
  27. offset: offset,
  28. name: string(id),
  29. }
  30. }
  31. // Position calculates and returns the absolute [position.Position].
  32. func (b *BetweenPoint) Position() position.Position {
  33. return position.Position{
  34. Vector: b.p.Vector().Add(b.inBetween()),
  35. Rotation: b.p.Vector().AngleBetween(b.q.Vector()) - math.Pi/2,
  36. }
  37. }
  38. func (b *BetweenPoint) inBetween() vector.Vector {
  39. return b.q.Vector().Subtract(b.p.Vector()).Multiply(b.offset)
  40. }
  41. // Vector calculates and returns the absolute [vector.Vector].
  42. func (b *BetweenPoint) Vector() vector.Vector {
  43. return b.Position().Vector
  44. }
  45. // Matrix calculates and returns the [canvas.Matrix] of a point.
  46. func (b *BetweenPoint) Matrix() canvas.Matrix {
  47. return b.p.Matrix().Translate(b.inBetween().Values()).
  48. Rotate((b.p.Vector().AngleBetween(b.q.Vector()) - math.Pi/2) * 180 / math.Pi)
  49. }
  50. // ID returns the point ID.
  51. func (b *BetweenPoint) ID() ID {
  52. return b.id
  53. }
  54. // Name returns the name of a point.
  55. func (b *BetweenPoint) Name() string {
  56. return b.name
  57. }
  58. // Draw returns if the point should be drawn.
  59. func (b *BetweenPoint) Draw() bool {
  60. return b.draw
  61. }
  62. // SetDraw indicates that the point should be drawn.
  63. func (b *BetweenPoint) SetDraw() {
  64. b.draw = true
  65. }
  66. // UnsetDraw indicates that the point should not be drawn.
  67. func (b *BetweenPoint) UnsetDraw() {
  68. b.draw = true
  69. }
  70. // Hide returns if the point must remain hidden.
  71. func (b *BetweenPoint) Hide() bool {
  72. return b.hide
  73. }
  74. // SetHide indicates that the must be hidden.
  75. func (b *BetweenPoint) SetHide() {
  76. b.hide = true
  77. }