Skip to content

Commit

Permalink
LineCurve/LineCurve3: Override getTangent() and getTangentAt(). (#…
Browse files Browse the repository at this point in the history
…25544)

* add tests `.getTangentAt`

* override `.getTangent*`

* use `.subVectors`

* revert unused fn par `_t` to `t`
  • Loading branch information
ycw authored Feb 23, 2023
1 parent 09cb168 commit f07b139
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/extras/curves/LineCurve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

}

Expand Down
13 changes: 13 additions & 0 deletions src/extras/curves/LineCurve3.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
7 changes: 6 additions & 1 deletion test/unit/src/extras/curves/LineCurve.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 ) => {
Expand Down

0 comments on commit f07b139

Please sign in to comment.