Source: draw/polygon.mjs

  1. import { Primitive } from "./primitive.mjs";
  2. /**
  3. * Primitive to draw a polygon shape.
  4. */
  5. export class PolygonPrimitive extends Primitive {
  6. vertices;
  7. constructor() {
  8. super();
  9. this.vertices = [];
  10. }
  11. /**
  12. * Push a vertex into the vertices list.
  13. * @param {number} x - the x coordinate of the vertex
  14. * @param {number} y - the y coordiante of the vertex
  15. * @returns {PolygonPrimitive}
  16. */
  17. vertex(x, y) {
  18. this.vertices.push({ x, y });
  19. return this;
  20. }
  21. /**
  22. * Push a list of vertices into the verticles list.
  23. * @param {Array} list - the list of vertices
  24. * @returns {PolygonPrimitive}
  25. */
  26. vertexList(list) {
  27. this.vertices.push(...list);
  28. return this;
  29. }
  30. vertexIndex(index, x, y) {
  31. this.vertices[index].x = { x, y };
  32. }
  33. /**
  34. * Create a quad polygon.
  35. * @param {number} ax a point x coord
  36. * @param {number} ay a point y coord
  37. * @param {number} bx b point x coord
  38. * @param {number} by b point y coord
  39. * @param {number} cx c point x coord
  40. * @param {number} cy c point y coord
  41. * @param {number} dx d point x coord
  42. * @param {number} dy d point y coord
  43. * @returns {PolygonPrimitive}
  44. */
  45. quad(ax, ay, bx, by, cx, cy, dx, dy) {
  46. this.vertex(ax, ay);
  47. this.vertex(bx, by);
  48. this.vertex(cx, cy);
  49. this.vertex(dx, dy);
  50. return this;
  51. }
  52. /**
  53. * Create a triangle polygon.
  54. * @param {number} ax a point x coord
  55. * @param {number} ay a point y coord
  56. * @param {number} bx b point x coord
  57. * @param {number} dy b point y coord
  58. * @param {number} cx c point x coord
  59. * @param {number} cy c point y coord
  60. * @returns {PolygonPrimitive}
  61. */
  62. tri(ax, ay, bx, dy, cx, cy) {
  63. this.vertex(ax, ay);
  64. this.vertex(bx, by);
  65. this.vertex(cx, cy);
  66. return this;
  67. }
  68. genCmdList() {
  69. let list = super.genCmdList();
  70. list.push(this);
  71. return list;
  72. }
  73. }