toosoon-utils
Version:
Utility functions & classes
72 lines (71 loc) • 1.74 kB
JavaScript
import { line } from '../../geometry';
import Curve from './Curve';
/**
* Utility class for manipulating lines
*
* @exports
* @class LineCurve
* @extends Curve
*/
export default class LineCurve extends Curve {
type = 'LineCurve';
/**
* X-axis coordinate of the start point
*/
x1;
/**
* Y-axis coordinate of the start point
*/
y1;
/**
* X-axis coordinate of the end point
*/
x2;
/**
* Y-axis coordinate of the end point
*/
y2;
/**
* @param {number} x1 X-axis coordinate of the start point
* @param {number} y1 Y-axis coordinate of the start point
* @param {number} x2 X-axis coordinate of the end point
* @param {number} y2 Y-axis coordinate of the end point
*/
constructor(x1, y1, x2, y2) {
super();
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
/**
* Interpolate a point on the line
*
* @param {number} t Normalized time value to interpolate
* @returns {Point} Interpolated coordinates on the line
*/
getPoint(t) {
const point = line(t, this.x1, this.y1, this.x2, this.y2);
return point;
}
getPointAt(u) {
return this.getPoint(u);
}
/**
* Compute an unit vector tangent for the given normalized time value
*
* @param {number} t Normalized time value
* @returns {[number, number]} Tangent vector
*/
getTangent(t) {
let x = this.x2 - this.x1;
let y = this.y2 - this.y1;
const length = Math.sqrt(x * x + y * y);
x /= length;
y /= length;
return [x, y];
}
getTangentAt(u) {
return this.getTangent(u);
}
}