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.

162 Zeilen
4.2KB

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