@openhps/core
Version:
Open Hybrid Positioning System - Core component
96 lines (90 loc) • 2.26 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.LineCurve = void 0;
var _Vector = require("../../math/Vector2.js");
var _Curve = require("../core/Curve.js");
/**
* A curve representing a 2D line segment.
*
* @augments Curve
*/
class LineCurve extends _Curve.Curve {
/**
* Constructs a new line curve.
*
* @param {Vector2} [v1] - The start point.
* @param {Vector2} [v2] - The end point.
*/
constructor(v1 = new _Vector.Vector2(), v2 = new _Vector.Vector2()) {
super();
/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isLineCurve = true;
this.type = 'LineCurve';
/**
* The start point.
*
* @type {Vector2}
*/
this.v1 = v1;
/**
* The end point.
*
* @type {Vector2}
*/
this.v2 = v2;
}
/**
* Returns a point on the line.
*
* @param {number} t - A interpolation factor representing a position on the line. Must be in the range `[0,1]`.
* @param {Vector2} [optionalTarget] - The optional target vector the result is written to.
* @return {Vector2} The position on the line.
*/
getPoint(t, optionalTarget = new _Vector.Vector2()) {
const point = optionalTarget;
if (t === 1) {
point.copy(this.v2);
} else {
point.copy(this.v2).sub(this.v1);
point.multiplyScalar(t).add(this.v1);
}
return point;
}
// Line curve is linear, so we can overwrite default getPointAt
getPointAt(u, optionalTarget) {
return this.getPoint(u, optionalTarget);
}
getTangent(t, optionalTarget = new _Vector.Vector2()) {
return optionalTarget.subVectors(this.v2, this.v1).normalize();
}
getTangentAt(u, optionalTarget) {
return this.getTangent(u, optionalTarget);
}
copy(source) {
super.copy(source);
this.v1.copy(source.v1);
this.v2.copy(source.v2);
return this;
}
toJSON() {
const data = super.toJSON();
data.v1 = this.v1.toArray();
data.v2 = this.v2.toArray();
return data;
}
fromJSON(json) {
super.fromJSON(json);
this.v1.fromArray(json.v1);
this.v2.fromArray(json.v2);
return this;
}
}
exports.LineCurve = LineCurve;