LineCurve.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { Vector2 } from '../../math/Vector2.js';
  2. import { Curve } from '../core/Curve.js';
  3. function LineCurve( v1 = new Vector2(), v2 = new Vector2() ) {
  4. Curve.call( this );
  5. this.type = 'LineCurve';
  6. this.v1 = v1;
  7. this.v2 = v2;
  8. }
  9. LineCurve.prototype = Object.create( Curve.prototype );
  10. LineCurve.prototype.constructor = LineCurve;
  11. LineCurve.prototype.isLineCurve = true;
  12. LineCurve.prototype.getPoint = function ( t, optionalTarget = new Vector2() ) {
  13. const point = optionalTarget;
  14. if ( t === 1 ) {
  15. point.copy( this.v2 );
  16. } else {
  17. point.copy( this.v2 ).sub( this.v1 );
  18. point.multiplyScalar( t ).add( this.v1 );
  19. }
  20. return point;
  21. };
  22. // Line curve is linear, so we can overwrite default getPointAt
  23. LineCurve.prototype.getPointAt = function ( u, optionalTarget ) {
  24. return this.getPoint( u, optionalTarget );
  25. };
  26. LineCurve.prototype.getTangent = function ( t, optionalTarget ) {
  27. const tangent = optionalTarget || new Vector2();
  28. tangent.copy( this.v2 ).sub( this.v1 ).normalize();
  29. return tangent;
  30. };
  31. LineCurve.prototype.copy = function ( source ) {
  32. Curve.prototype.copy.call( this, source );
  33. this.v1.copy( source.v1 );
  34. this.v2.copy( source.v2 );
  35. return this;
  36. };
  37. LineCurve.prototype.toJSON = function () {
  38. const data = Curve.prototype.toJSON.call( this );
  39. data.v1 = this.v1.toArray();
  40. data.v2 = this.v2.toArray();
  41. return data;
  42. };
  43. LineCurve.prototype.fromJSON = function ( json ) {
  44. Curve.prototype.fromJSON.call( this, json );
  45. this.v1.fromArray( json.v1 );
  46. this.v2.fromArray( json.v2 );
  47. return this;
  48. };
  49. export { LineCurve };