Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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