Skip to content

Commit

Permalink
feature: add point class (closes #76)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebeamish committed Dec 7, 2024
1 parent ffa1343 commit 6b6bfde
Show file tree
Hide file tree
Showing 6 changed files with 549 additions and 450 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Documentation for Triangle class.
- Tags in changelog link to github.
- Get centroid and midpoints methods for Triangle class.
- Point class that can be drawn as a tiny circle or tiny line.

### Fixed

Expand Down
16 changes: 14 additions & 2 deletions source/Plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Line } from "./Line.js";
import { Circle } from "./Circle.js";
import { Path } from "./Path.js";
import { SVGBuilder } from "./SVGBuilder.js";

import { Point } from "./Point.js";
/**
* @class
* Plot is an object that is able to create, display and download SVG documents.
Expand Down Expand Up @@ -48,6 +48,8 @@ export class Plot {
this.lines = [];
this.paths = [];
this.circles = [];
this.points = [];

this.seedHistory = [];

this.svgBuilder = new SVGBuilder();
Expand Down Expand Up @@ -121,9 +123,11 @@ plot.draw();
this.paths.push(shape);
} else if (shape instanceof Circle) {
this.circles.push(shape);
} else if (shape instanceof Point){
this.points.push(shape);
} else {
throw new TypeError(
"Unsupported shape type. Shape must be a Line, Path or Circle.",
"Unsupported shape type. Shape must be a Line, Path, Point or Circle.",
);
}
}
Expand All @@ -146,6 +150,8 @@ plot.draw();

this.addCirclesToSVG();

this.addPointsToSVG();

this.svg = this.svgBuilder.build();

const timeTaken = +(
Expand Down Expand Up @@ -317,6 +323,12 @@ plot.draw();
}
}

addPointsToSVG() {
for (const point of this.points) {
this.svgBuilder.addShape(point.toSVGElement());
}
}

/**
* Appends this plot's SVG element to the document body.
*/
Expand Down
35 changes: 35 additions & 0 deletions source/Point.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Vector } from "./Vector.js";
import { Circle } from "./Circle.js";
import { Line } from "./Line.js";
/**
* Class representing a Point.
*/
export class Point {
/**
* Create a Point.
* @param {Vector} position
* @param {string} [style = "circle"]
*/
constructor(position, length = 0.25, style = "circle") {
this.position = position;
this.style = style;
this.length = length;
}

/**
* @returns {SVGElement}
*/
toSVGElement() {
let shape;
switch (this.style) {
case "circle":
shape = new Circle(this.position.x, this.position.y, this.length);
return shape.toSVGElement();
case "line":
let length = new Vector(this.length, 0);
let direction = 0;
shape = new Line(this.position, Vector.add(this.position, length));
return shape.toSVGElement();
}
}
}
1 change: 1 addition & 0 deletions source/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { Vector } from "./Vector.js";
export { Point } from "./Point.js";
export { Line } from "./Line.js";
export { Path } from "./Path.js";
export { Circle } from "./Circle.js";
Expand Down
Loading

0 comments on commit 6b6bfde

Please sign in to comment.