QuadraticBezierCurve.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { Curve } from '../core/Curve';
  2. import { Vector2 } from '../../math/Vector2';
  3. import { CurveUtils } from '../CurveUtils';
  4. import { ShapeUtils } from '../ShapeUtils';
  5. /**************************************************************
  6. * Quadratic Bezier curve
  7. **************************************************************/
  8. function QuadraticBezierCurve( v0, v1, v2 ) {
  9. this.isQuadraticBezierCurve = this.isCurve = true;
  10. this.v0 = v0;
  11. this.v1 = v1;
  12. this.v2 = v2;
  13. };
  14. QuadraticBezierCurve.prototype = Object.create( Curve.prototype );
  15. QuadraticBezierCurve.prototype.constructor = QuadraticBezierCurve;
  16. QuadraticBezierCurve.prototype.getPoint = function ( t ) {
  17. var b2 = ShapeUtils.b2;
  18. return new Vector2(
  19. b2( t, this.v0.x, this.v1.x, this.v2.x ),
  20. b2( t, this.v0.y, this.v1.y, this.v2.y )
  21. );
  22. };
  23. QuadraticBezierCurve.prototype.getTangent = function( t ) {
  24. var tangentQuadraticBezier = CurveUtils.tangentQuadraticBezier;
  25. return new Vector2(
  26. tangentQuadraticBezier( t, this.v0.x, this.v1.x, this.v2.x ),
  27. tangentQuadraticBezier( t, this.v0.y, this.v1.y, this.v2.y )
  28. ).normalize();
  29. };
  30. export { QuadraticBezierCurve };