From f07b1396ee52aa6fd8063d327e96a1dbcde79ac3 Mon Sep 17 00:00:00 2001 From: ycw Date: Thu, 23 Feb 2023 12:20:02 +0800 Subject: [PATCH] LineCurve/LineCurve3: Override `getTangent()` and `getTangentAt()`. (#25544) * add tests `.getTangentAt` * override `.getTangent*` * use `.subVectors` * revert unused fn par `_t` to `t` --- src/extras/curves/LineCurve.js | 10 ++++++---- src/extras/curves/LineCurve3.js | 13 +++++++++++++ test/unit/src/extras/curves/LineCurve.tests.js | 7 ++++++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/extras/curves/LineCurve.js b/src/extras/curves/LineCurve.js index 4b14d5edb6aad8..0a636898ce30cb 100644 --- a/src/extras/curves/LineCurve.js +++ b/src/extras/curves/LineCurve.js @@ -42,13 +42,15 @@ class LineCurve extends Curve { } - getTangent( t, optionalTarget ) { + getTangent( t, optionalTarget = new Vector2() ) { - const tangent = optionalTarget || new Vector2(); + return optionalTarget.subVectors( this.v2, this.v1 ).normalize(); - tangent.copy( this.v2 ).sub( this.v1 ).normalize(); + } + + getTangentAt( u, optionalTarget ) { - return tangent; + return this.getTangent( u, optionalTarget ); } diff --git a/src/extras/curves/LineCurve3.js b/src/extras/curves/LineCurve3.js index c315c3869f3024..f83a63914fed22 100644 --- a/src/extras/curves/LineCurve3.js +++ b/src/extras/curves/LineCurve3.js @@ -39,6 +39,19 @@ class LineCurve3 extends Curve { return this.getPoint( u, optionalTarget ); } + + getTangent( t, optionalTarget = new Vector3() ) { + + return optionalTarget.subVectors( this.v2, this.v1 ).normalize(); + + } + + getTangentAt( u, optionalTarget ) { + + return this.getTangent( u, optionalTarget ); + + } + copy( source ) { super.copy( source ); diff --git a/test/unit/src/extras/curves/LineCurve.tests.js b/test/unit/src/extras/curves/LineCurve.tests.js index 04ce06a6b02d26..3fff1b3f477e1f 100644 --- a/test/unit/src/extras/curves/LineCurve.tests.js +++ b/test/unit/src/extras/curves/LineCurve.tests.js @@ -106,7 +106,7 @@ export default QUnit.module( 'Extras', () => { } ); - QUnit.test( 'getTangent', ( assert ) => { + QUnit.test( 'getTangent/getTangentAt', ( assert ) => { const curve = _curve; const tangent = new Vector2(); @@ -117,6 +117,11 @@ export default QUnit.module( 'Extras', () => { assert.numEqual( tangent.x, expectedTangent, 'tangent.x correct' ); assert.numEqual( tangent.y, expectedTangent, 'tangent.y correct' ); + curve.getTangentAt( 0, tangent ); + + assert.numEqual( tangent.x, expectedTangent, 'tangentAt.x correct' ); + assert.numEqual( tangent.y, expectedTangent, 'tangentAt.y correct' ); + } ); QUnit.todo( 'copy', ( assert ) => {