UNPKG

@cantoo/pdf-lib

Version:

Create and modify PDF files with JavaScript

42 lines 1.46 kB
import { plus } from '../maths.js'; import GraphElement from './GraphElement.js'; import Point from './Point.js'; import Segment from './Segment.js'; export default class Plot extends GraphElement { constructor(points = []) { super(); this.points = points; } getPoints() { return [...this.points]; } translate(translationVector) { this.points = this.points.map((point) => plus(point, translationVector)); } isEqual(element) { if (!(element instanceof Plot)) return false; const points = this.getPoints().map((coord) => new Point(coord)); const points2 = element.getPoints().map((coord) => new Point(coord)); return (points.every((point, i) => point.isEqual(points2[i])) || points.reverse().every((point, i) => point.isEqual(points2[i]))); } orthoProjection(P) { const points = this.getPoints(); const orthos = points .slice(0, -1) .map((pt, i) => new Segment(new Point(pt), new Point(points[i + 1]))) .map((seg) => seg.orthoProjection(P)); let min = Number.POSITIVE_INFINITY; let closest = new Point(points[0]); orthos.forEach((ortho) => { const d = ortho.distance(P); if (d < min) { min = d; closest = ortho; } }); return closest; } } //# sourceMappingURL=Plot.js.map