Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

161 строка
4.1KB

  1. package point
  2. import (
  3. "math"
  4. "git.wtrh.nl/patterns/gopatterns/pkg/position"
  5. "git.wtrh.nl/patterns/gopatterns/pkg/vector"
  6. "github.com/tdewolff/canvas"
  7. )
  8. // RelativePoint implements Point and defines a position that is relative to a different Point.
  9. type RelativePoint struct {
  10. draw bool
  11. name string
  12. point Point
  13. relativeOffset position.Position
  14. id ID
  15. hide bool
  16. }
  17. // Name returns the name of a point.
  18. func (r *RelativePoint) Name() string {
  19. return r.name
  20. }
  21. // Done returns the relativePoint as interface.
  22. func (r *RelativePoint) Done() Point { //nolint:ireturn
  23. return r
  24. }
  25. // MarkWith sets the ID of the point.
  26. func (r *RelativePoint) MarkWith(n ID) *RelativePoint {
  27. r.id = n
  28. if r.name == "" {
  29. r.name = string(n)
  30. }
  31. return r
  32. }
  33. // Position calculates and returns the absolute [position.Position].
  34. func (r *RelativePoint) Position() position.Position {
  35. p := r.point.Position()
  36. return p.Add(r.relativeOffset)
  37. }
  38. // Matrix calculates and returns the [canvas.Matrix] of a point.
  39. func (r *RelativePoint) Matrix() canvas.Matrix {
  40. return r.point.Matrix().Translate(r.relativeOffset.Vector.Values()).Rotate(r.relativeOffset.Rotation / math.Pi * 180)
  41. }
  42. // Vector calculates and returns the absolute [vector.Vector].
  43. func (r *RelativePoint) Vector() vector.Vector {
  44. return r.Position().Vector
  45. }
  46. // ID returns the point ID.
  47. func (r *RelativePoint) ID() ID {
  48. return r.id
  49. }
  50. // NewRelativePointWithVector returns a new RelativePoint.
  51. func NewRelativePointWithVector(point Point, p vector.Vector, id ID) *RelativePoint {
  52. return &RelativePoint{
  53. point: point,
  54. relativeOffset: position.Position{Vector: p},
  55. id: id,
  56. name: string(id),
  57. }
  58. }
  59. // NewRelativePoint returns a new RelativePoint.
  60. func NewRelativePoint(point Point) *RelativePoint {
  61. return &RelativePoint{
  62. point: point,
  63. }
  64. }
  65. // NewRotationPoint returns a new RelativePoint with a specific rotation.
  66. func NewRotationPoint(point Point, a float64, id ID) *RelativePoint {
  67. p := &RelativePoint{
  68. point: point,
  69. relativeOffset: position.Position{
  70. Vector: vector.Vector{},
  71. Rotation: a,
  72. },
  73. name: string(id),
  74. id: id,
  75. }
  76. return p
  77. }
  78. // WithXOffset sets the x value for the relative offset.
  79. func (r *RelativePoint) WithXOffset(value float64) *RelativePoint {
  80. r.relativeOffset.Vector.X = value
  81. return r
  82. }
  83. // WithYOffset sets the y value for the relative offset.
  84. func (r *RelativePoint) WithYOffset(value float64) *RelativePoint {
  85. r.relativeOffset.Vector.Y = value
  86. return r
  87. }
  88. // WithRotationOffset sets the angle for the relative offset.
  89. func (r *RelativePoint) WithRotationOffset(value float64) *RelativePoint {
  90. r.relativeOffset.Rotation = value
  91. return r
  92. }
  93. // NewRelativePointBelow returns a RelativePoint distance f below another Point.
  94. func NewRelativePointBelow(point Point, f float64, id ID) *RelativePoint {
  95. return NewRelativePointWithVector(point, vector.Vector{X: 0, Y: -f}, id)
  96. }
  97. // NewRelativePointAbove returns a RelativePoint distance f above another Point.
  98. func NewRelativePointAbove(point Point, f float64, id ID) *RelativePoint {
  99. return NewRelativePointWithVector(point, vector.Vector{X: 0, Y: f}, id)
  100. }
  101. // NewRelativePointLeft returns a RelativePoint distance f left of another Point.
  102. func NewRelativePointLeft(point Point, f float64, id ID) *RelativePoint {
  103. return NewRelativePointWithVector(point, vector.Vector{X: -f, Y: 0}, id)
  104. }
  105. // NewRelativePointRight returns a RelativePoint distance f right of another Point.
  106. func NewRelativePointRight(point Point, f float64, id ID) *RelativePoint {
  107. return NewRelativePointWithVector(point, vector.Vector{X: f, Y: 0}, id)
  108. }
  109. // Draw returns if the point should be drawn.
  110. func (r *RelativePoint) Draw() bool {
  111. return r.draw
  112. }
  113. // SetDraw indicates that the point should be drawn.
  114. func (r *RelativePoint) SetDraw() {
  115. r.draw = true
  116. }
  117. // UnsetDraw indicates that the point should not be drawn.
  118. func (r *RelativePoint) UnsetDraw() {
  119. r.draw = true
  120. }
  121. // Hide returns if the point must remain hidden.
  122. func (r *RelativePoint) Hide() bool {
  123. return r.hide
  124. }
  125. // SetHide indicates that the must be hidden.
  126. func (r *RelativePoint) SetHide() {
  127. r.hide = true
  128. }