No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

93 líneas
2.3KB

  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. // 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 canvas.Identity.Translate(b.Vector().Values()).Rotate(b.Position().RotationD())
  49. }
  50. // ID returns the point ID.
  51. func (b *BetweenPoint) ID() util.ID {
  52. return b.id
  53. }
  54. // Name returns the name of a point.
  55. func (b *BetweenPoint) Name() string {
  56. return b.name
  57. }
  58. // Draw returns if the point should be drawn.
  59. func (b *BetweenPoint) Draw() bool {
  60. return b.draw
  61. }
  62. // SetDraw indicates that the point should be drawn.
  63. func (b *BetweenPoint) SetDraw() {
  64. b.draw = true
  65. }
  66. // UnsetDraw indicates that the point should not be drawn.
  67. func (b *BetweenPoint) UnsetDraw() {
  68. b.draw = true
  69. }
  70. // Hide returns if the point must remain hidden.
  71. func (b *BetweenPoint) Hide() bool {
  72. return b.hide
  73. }
  74. // SetHide indicates that the must be hidden.
  75. func (b *BetweenPoint) SetHide() {
  76. b.hide = true
  77. }