@cantoo/pdf-lib
Version:
Create and modify PDF files with JavaScript
76 lines • 2.49 kB
JavaScript
import { distance, isColinear, norm, scalar, vector, plus, times, } from '../maths.js';
import GraphElement from './GraphElement.js';
import Line from './Line.js';
import Point from './Point.js';
export default class Segment extends GraphElement {
constructor(A = new Point(), B = new Point()) {
super();
this.A = A;
this.B = B;
}
origin() {
return this.A;
}
destination() {
return this.B;
}
dirVect() {
return vector(this.origin(), this.destination());
}
length() {
return distance(this.destination(), this.origin());
}
isEqual(element) {
if (!(element instanceof Segment))
return false;
const o = this.origin();
const dest = this.destination();
const oE = element.origin();
const destE = element.destination();
return (element instanceof Segment &&
((o.isEqual(oE) && dest.isEqual(destE)) ||
(o.isEqual(destE) && dest.isEqual(oE))));
}
/** Returns an equivalent line object */
getLine() {
const line = new Line(this.origin(), this.destination());
return line;
}
includes(P) {
const vect = this.dirVect();
const otherVect = vector(this.origin(), P);
// The vectors are not even colinear
if (!isColinear(vect, otherVect))
return false;
// The point is behind the origin
else if (scalar(vect, otherVect) < 0)
return false;
// The point is after the destination
else if (norm(vect) < norm(otherVect))
return false;
else
return true;
}
middle() {
const mid = new Point(plus(this.origin().toCoords(), times(this.dirVect(), 0.5)));
return mid;
}
orthoProjection(P) {
const H = this.getLine().orthoProjection(P);
const vect = this.dirVect();
const origin = this.origin().toCoords();
const destination = this.destination().toCoords();
const otherVect = vector(this.origin(), H);
// The point is before the origin
if (scalar(vect, otherVect) < 0)
return new Point(origin);
// The point is after the destination
else if (norm(vect) < norm(otherVect))
return new Point(destination);
// The point is within the segment
else
return H;
}
}
Segment.type = 'Segment';
//# sourceMappingURL=Segment.js.map