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.

94 lines
2.4KB

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