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.

81 line
2.2KB

  1. // Package point contains different types of points for drawing patterns.
  2. // There are points that define an absolute position or a relative position.
  3. // Relative can be a specific distance one point or between two other points.
  4. package point
  5. import (
  6. "git.wtrh.nl/patterns/gopatterns/pkg/position"
  7. "git.wtrh.nl/patterns/gopatterns/pkg/vector"
  8. "github.com/tdewolff/canvas"
  9. )
  10. // ID defines a point id.
  11. type ID string
  12. // Point defines the interface for different types of points.
  13. type Point interface {
  14. // ID returns the point ID.
  15. ID() ID
  16. // Position calculates and returns the absolute [position.Position].
  17. Position() position.Position
  18. // Vector calculates and returns the absolute [vector.Vector].
  19. Vector() vector.Vector
  20. // Matrix calculates and returns the [canvas.Matrix] of a point.
  21. Matrix() canvas.Matrix
  22. // Name returns the name of a point.
  23. Name() string
  24. // SetDraw indicates that the point should be drawn.
  25. SetDraw()
  26. // Draw returns if the point should be drawn.
  27. Draw() bool
  28. // Hide returns if the point must remain hidden.
  29. Hide() bool
  30. // SetHide indicates that the must be hidden.
  31. SetHide()
  32. }
  33. // Draw draws a specific Point to the provided [canvas.Canvas].
  34. // The point is only drawn when the point states that it should be drawn and not must be hidden.
  35. func Draw(c *canvas.Canvas, point Point, face *canvas.FontFace, debug bool) {
  36. if !debug && (!point.Draw() || point.Hide()) {
  37. return
  38. }
  39. path := canvas.Circle(1.0)
  40. m := point.Matrix()
  41. style := canvas.Style{Fill: canvas.Paint{Color: canvas.Black}}
  42. c.RenderPath(path, style, m)
  43. text := canvas.NewTextLine(face, point.Name(), canvas.Bottom)
  44. c.RenderText(text, m.Translate(2, -4))
  45. if debug {
  46. yStyle := canvas.Style{
  47. Fill: canvas.Paint{},
  48. Stroke: canvas.Paint{Color: canvas.Green},
  49. StrokeWidth: 0.2,
  50. StrokeCapper: canvas.RoundCap,
  51. StrokeJoiner: canvas.BevelJoin,
  52. }
  53. yLine := canvas.Line(0, 10)
  54. xStyle := canvas.Style{
  55. Fill: canvas.Paint{},
  56. Stroke: canvas.Paint{Color: canvas.Red},
  57. StrokeWidth: 0.2,
  58. StrokeCapper: canvas.RoundCap,
  59. StrokeJoiner: canvas.BevelJoin,
  60. }
  61. xLine := canvas.Line(10, 0)
  62. c.RenderPath(yLine, yStyle, m)
  63. c.RenderPath(xLine, xStyle, m)
  64. }
  65. }