Skip to content

Commit 4eccb8f

Browse files
committed
added clampLength method to Vector2 and Vector3
1 parent e2a72e1 commit 4eccb8f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/math/Vector2.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,28 @@ THREE.Vector2.prototype = {
290290

291291
}(),
292292

293+
clampLength: function () {
294+
295+
var oldLength, newLength;
296+
297+
return function clampLength( min, max ) {
298+
299+
oldLength = this.length();
300+
301+
newLength = ( oldLength < min ) ? min : ( ( oldLength > max ) ? max : oldLength );
302+
303+
if ( newLength !== oldLength ) {
304+
305+
this.multiplyScalar( newLength / oldLength );
306+
307+
}
308+
309+
return this;
310+
311+
};
312+
313+
}(),
314+
293315
floor: function () {
294316

295317
this.x = Math.floor( this.x );

src/math/Vector3.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,28 @@ THREE.Vector3.prototype = {
520520

521521
}(),
522522

523+
clampLength: function () {
524+
525+
var oldLength, newLength;
526+
527+
return function clampLength( min, max ) {
528+
529+
oldLength = this.length();
530+
531+
newLength = ( oldLength < min ) ? min : ( ( oldLength > max ) ? max : oldLength );
532+
533+
if ( newLength !== oldLength ) {
534+
535+
this.multiplyScalar( newLength / oldLength );
536+
537+
}
538+
539+
return this;
540+
541+
};
542+
543+
}(),
544+
523545
floor: function () {
524546

525547
this.x = Math.floor( this.x );

0 commit comments

Comments
 (0)