toosoon-utils
Version:
Utility functions & classes
78 lines (77 loc) • 2.32 kB
JavaScript
import { TWO_PI } from '../../constants';
import { ellipse } from '../../geometry';
import Curve from './Curve';
/**
* Utility class for manipulating ellipses
*
* @exports
* @class EllipseCurve
* @extends Curve
*/
export default class EllipseCurve extends Curve {
type = 'EllipseCurve';
/**
* X-axis coordinate of the center of the ellipse
*/
cx;
/**
* Y-axis coordinate of the center of the ellipse
*/
cy;
/**
* X-radius of the ellipse
*/
rx;
/**
* Y-radius of the ellipse
*/
ry;
/**
* Rotation angle of the ellipse (in radians), counterclockwise from the positive X-axis
*/
rotation;
/**
* Start angle of the arc (in radians)
*/
startAngle;
/**
* End angle of the arc (in radians)
*/
endAngle;
/**
* Flag indicating the direction of the arc
*/
counterclockwise;
/**
* @param {number} cx X-axis coordinate of the center of the ellipse
* @param {number} cy Y-axis coordinate of the center of the ellipse
* @param {number} rx X-radius of the ellipse
* @param {number} ry Y-radius of the ellipse
* @param {number} [rotation] Rotation angle of the ellipse (in radians), counterclockwise from the positive X-axis
* @param {number} [startAngle] Rotation angle of the arc (in radians)
* @param {number} [endAngle] Rotation angle of the arc (in radians)
* @param {boolean} [counterclockwise] Flag indicating the direction of the arc
*/
constructor(cx, cy, rx, ry, rotation, startAngle, endAngle, counterclockwise) {
super();
this.cx = cx;
this.cy = cy;
this.rx = rx;
this.ry = ry;
this.rotation = rotation ?? 0;
this.startAngle = startAngle ?? 0;
this.endAngle = endAngle ?? TWO_PI;
this.counterclockwise = counterclockwise ?? false;
}
/**
* Interpolate a point on the Ellipse curve
*
* @abstract
* @param {number} t Normalized time value to interpolate
* @returns {Point} Interpolated coordinates on the curve
*/
getPoint(t) {
const point = ellipse(t, this.cx, this.cy, this.rx, this.ry, this.rotation, this.startAngle, this.endAngle, this.counterclockwise);
return point;
}
}