LineCurve.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.type = 'LineCurve';
  6. this.v1 = v1 || new Vector2();
  7. this.v2 = v2 || new Vector2();
  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 ) {
  13. var point = optionalTarget || new Vector2();
  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 */ ) {
  27. var tangent = this.v2.clone().sub( this.v1 );
  28. return tangent.normalize();
  29. };
  30. LineCurve.prototype.copy = function ( source ) {
  31. Curve.prototype.copy.call( this, source );
  32. this.v1.copy( source.v1 );
  33. this.v2.copy( source.v2 );
  34. return this;
  35. };
  36. LineCurve.prototype.toJSON = function () {
  37. var data = Curve.prototype.toJSON.call( this );
  38. data.v1 = this.v1.toArray();
  39. data.v2 = this.v2.toArray();
  40. return data;
  41. };
  42. LineCurve.prototype.fromJSON = function ( json ) {
  43. Curve.prototype.fromJSON.call( this, json );
  44. this.v1.fromArray( json.v1 );
  45. this.v2.fromArray( json.v2 );
  46. return this;
  47. };
  48. export { LineCurve };