Source: Point.js

  1. import { Vector } from "./Vector.js";
  2. import { Circle } from "./Circle.js";
  3. import { Line } from "./Line.js";
  4. /**
  5. * Class representing a Point.
  6. *
  7. * A Point is a small plottable shape that is drawn as either a {@link Circle}
  8. * by default, or a very short {@link Line}.
  9. *
  10. * Points can be instantiated with a vector position.
  11. */
  12. export class Point {
  13. /**
  14. * Create a Point.
  15. *
  16. * Points are constructed from a {@link Vector} position, a size, and a style.
  17. *
  18. * @param {Vector} position - The position of the Point as a {@link Vector}.
  19. * @param {Number} [length = 0.25] - The radius or length of the drawn Point.
  20. * @param {string} [style = "circle"] - Shape type ("circle" or "line").
  21. */
  22. constructor(position, length = 0.25, style = "circle") {
  23. this.position = position;
  24. this.style = style;
  25. this.length = length;
  26. }
  27. /**
  28. * Converts the Point to an SVGElement.
  29. *
  30. * This method is used by {@link Plot#add}.
  31. * @returns {SVGElement}
  32. */
  33. toSVGElement() {
  34. let shape;
  35. switch (this.style) {
  36. case "circle":
  37. shape = new Circle(this.position.x, this.position.y, this.length);
  38. return shape.toSVGElement();
  39. case "line":
  40. // let length = // let direction = 0;
  41. shape = new Line(this.position, Vector.add(this.position, new Vector(this.length, 0)
  42. ));
  43. return shape.toSVGElement();
  44. }
  45. }
  46. }