UNPKG

toosoon-utils

Version:
103 lines (102 loc) 3.25 kB
import { quadraticBezier } from '../../maths'; import { Vector3 } from '../geometry'; import Curve from './Curve'; /** * Utility class for manipulating Quadratic Bézier 3D curves * * @exports * @class QuadraticBezierCurve3 * @extends Curve */ export default class QuadraticBezierCurve3 extends Curve { type = 'QuadraticBezierCurve3'; /** * X-axis coordinate of the start point */ x1; /** * Y-axis coordinate of the start point */ y1; /** * Z-axis coordinate of the start point */ z1; /** * X-axis coordinate of the control point */ cpx; /** * Y-axis coordinate of the control point */ cpy; /** * Z-axis coordinate of the control point */ cpz; /** * X-axis coordinate of the end point */ x2; /** * Y-axis coordinate of the end point */ y2; /** * Z-axis coordinate of the end point */ z2; /** * @param {number} x1 X-axis coordinate of the start point * @param {number} y1 Y-axis coordinate of the start point * @param {number} z1 Z-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} cpz Z-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 * @param {number} z2 Z-axis coordinate of the end point */ constructor(x1, y1, z1, cpx, cpy, cpz, x2, y2, z2) { super(); this.x1 = x1; this.y1 = y1; this.z1 = z1; this.cpx = cpx; this.cpy = cpy; this.cpz = cpz; this.x2 = x2; this.y2 = y2; this.z2 = z2; } /** * Interpolate a point on this curve * * @param {number} t Normalized time value to interpolate * @returns {Vector3} Interpolated coordinates on this curve */ getPoint(t) { return new Vector3(...QuadraticBezierCurve3.interpolate(t, this.x1, this.y1, this.z1, this.cpx, this.cpy, this.cpz, this.x2, this.y2, this.z2)); } /** * Interpolate a point on a Quadratic Bézier 3D 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} z1 Z-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} cpz Z-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 * @param {number} z2 Z-axis coordinate of the end point * @returns {Point3} Interpolated coordinates on the curve */ static interpolate(t, x1, y1, z1, cpx, cpy, cpz, x2, y2, z2) { const x = quadraticBezier(t, x1, cpx, x2); const y = quadraticBezier(t, y1, cpy, y2); const z = quadraticBezier(t, z1, cpz, z2); return [x, y, z]; } }