You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.2KB

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