選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

94 行
2.3KB

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