Browse Source

added clampLength method to Vector2 and Vector3

Mugen87 9 years ago
parent
commit
4eccb8f1d8
2 changed files with 44 additions and 0 deletions
  1. 22 0
      src/math/Vector2.js
  2. 22 0
      src/math/Vector3.js

+ 22 - 0
src/math/Vector2.js

@@ -290,6 +290,28 @@ THREE.Vector2.prototype = {
 
 	}(),
 
+	clampLength: function () {
+
+		var oldLength, newLength;
+
+		return function clampLength( min, max ) {
+
+			oldLength = this.length();
+
+			newLength = ( oldLength < min ) ? min : ( ( oldLength > max ) ? max : oldLength );
+
+			if ( newLength !== oldLength ) {
+
+				this.multiplyScalar( newLength / oldLength );
+
+			}
+
+			return this;
+
+		};
+
+	}(),
+
 	floor: function () {
 
 		this.x = Math.floor( this.x );

+ 22 - 0
src/math/Vector3.js

@@ -520,6 +520,28 @@ THREE.Vector3.prototype = {
 
 	}(),
 
+	clampLength: function () {
+
+		var oldLength, newLength;
+
+		return function clampLength( min, max ) {
+
+			oldLength = this.length();
+
+			newLength = ( oldLength < min ) ? min : ( ( oldLength > max ) ? max : oldLength );
+
+			if ( newLength !== oldLength ) {
+
+				this.multiplyScalar( newLength / oldLength );
+
+			}
+
+			return this;
+
+		};
+
+	}(),
+
 	floor: function () {
 
 		this.x = Math.floor( this.x );