LineCurve.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { Vector2 } from '../../math/Vector2.js';
  2. import { Curve } from '../core/Curve.js';
  3. function LineCurve( v1, v2 ) {
  4. Curve.call( this );
  5. this.v1 = v1 || new Vector2();
  6. this.v2 = v2 || new Vector2();
  7. }
  8. LineCurve.prototype = Object.create( Curve.prototype );
  9. LineCurve.prototype.constructor = LineCurve;
  10. LineCurve.prototype.isLineCurve = true;
  11. LineCurve.prototype.getPoint = function ( t, optionalTarget ) {
  12. var point = optionalTarget || new Vector2();
  13. if ( t === 1 ) {
  14. point.copy( this.v2 );
  15. } else {
  16. point.copy( this.v2 ).sub( this.v1 );
  17. point.multiplyScalar( t ).add( this.v1 );
  18. }
  19. return point;
  20. };
  21. // Line curve is linear, so we can overwrite default getPointAt
  22. LineCurve.prototype.getPointAt = function ( u, optionalTarget ) {
  23. return this.getPoint( u, optionalTarget );
  24. };
  25. LineCurve.prototype.getTangent = function ( /* t */ ) {
  26. var tangent = this.v2.clone().sub( this.v1 );
  27. return tangent.normalize();
  28. };
  29. LineCurve.prototype.copy = function ( source ) {
  30. Curve.prototype.copy.call( this, source );
  31. this.v1.copy( source.v1 );
  32. this.v2.copy( source.v2 );
  33. return this;
  34. };
  35. export { LineCurve };