Skip to content

Commit 3b8338c

Browse files
committed
more compact version of clampLength
1 parent c4c85e8 commit 3b8338c

File tree

2 files changed

+4
-16
lines changed

2 files changed

+4
-16
lines changed

src/math/Vector2.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,9 @@ THREE.Vector2.prototype = {
292292

293293
clampLength: function ( min, max ) {
294294

295-
var oldLength = this.length();
296-
297-
var newLength = ( oldLength < min ) ? min : ( ( oldLength > max ) ? max : oldLength );
298-
299-
if ( oldLength !== 0 && newLength !== oldLength ) {
295+
var length = this.length();
300296

301-
this.multiplyScalar( newLength / oldLength );
302-
303-
}
297+
this.multiplyScalar( Math.max( min, Math.min( max, length ) ) / length );
304298

305299
return this;
306300

src/math/Vector3.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -522,15 +522,9 @@ THREE.Vector3.prototype = {
522522

523523
clampLength: function ( min, max ) {
524524

525-
var oldLength = this.length();
526-
527-
var newLength = ( oldLength < min ) ? min : ( ( oldLength > max ) ? max : oldLength );
528-
529-
if ( oldLength !== 0 && newLength !== oldLength ) {
525+
var length = this.length();
530526

531-
this.multiplyScalar( newLength / oldLength );
532-
533-
}
527+
this.multiplyScalar( Math.max( min, Math.min( max, length ) ) / length );
534528

535529
return this;
536530

0 commit comments

Comments
 (0)