toosoon-utils
Version:
Utility functions & classes
81 lines (80 loc) • 2.5 kB
JavaScript
import { quadraticBezier } from '../../math';
import { Vector2 } from '../geometry';
import Curve from './Curve';
/**
* Utility class for manipulating Quadratic Bézier curves
*
* @exports
* @class QuadraticBezierCurve
* @extends Curve<Vector2>
*/
export default class QuadraticBezierCurve extends Curve {
type = 'QuadraticBezierCurve';
/**
* X-axis coordinate of the start point
*/
x1;
/**
* Y-axis coordinate of the start point
*/
y1;
/**
* X-axis coordinate of the control point
*/
cpx;
/**
* Y-axis coordinate of the control point
*/
cpy;
/**
* 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} cpx X-axis coordinate of the control point
* @param {number} cpy Y-axis coordinate of the control point
* @param {number} x2 X-axis coordinate of the end point
* @param {number} y2 Y-axis coordinate of the end point
*/
constructor(x1, y1, cpx, cpy, x2, y2) {
super();
this.x1 = x1;
this.y1 = y1;
this.cpx = cpx;
this.cpy = cpy;
this.x2 = x2;
this.y2 = y2;
}
/**
* Interpolate a point on this Quadratic Bézier curve
*
* @param {number} t Normalized time value to interpolate
* @returns {Vector2} Interpolated coordinates on this curve
*/
getPoint(t) {
return new Vector2(...QuadraticBezierCurve.interpolate(t, this.x1, this.y1, this.cpx, this.cpy, this.x2, this.y2));
}
/**
* Interpolate a point on a Quadratic Bézier curve
*
* @param {number} t Normalized time value to interpolate
* @param {number} x1 X-axis coordinate of the start point
* @param {number} y1 Y-axis coordinate of the start point
* @param {number} cpx X-axis coordinate of the control point
* @param {number} cpy Y-axis coordinate of the control point
* @param {number} x2 X-axis coordinate of the end point
* @param {number} y2 Y-axis coordinate of the end point
* @returns {Point2} Interpolated coordinates on the curve
*/
static interpolate(t, x1, y1, cpx, cpy, x2, y2) {
const x = quadraticBezier(t, x1, cpx, x2);
const y = quadraticBezier(t, y1, cpy, y2);
return [x, y];
}
}