Browse Source

Merge pull request #11253 from WestLangley/dev-vector-math

Vector2/3/4: Avoid divide-by-zero
Mr.doob 8 years ago
parent
commit
c282b17090
4 changed files with 36 additions and 13 deletions
  1. 9 0
      docs/api/math/Vector4.html
  2. 4 4
      src/math/Vector2.js
  3. 4 4
      src/math/Vector3.js
  4. 19 5
      src/math/Vector4.js

+ 9 - 0
docs/api/math/Vector4.html

@@ -110,6 +110,15 @@ var d = a.dot( b );
 		If this vector's x, y, z or w value is less than the min vector's x, y, z or w value, it is replaced by the corresponding value.
 		If this vector's x, y, z or w value is less than the min vector's x, y, z or w value, it is replaced by the corresponding value.
 		</div>
 		</div>
 
 
+		<h3>[method:Vector4 clampLength]( [page:Float min], [page:Float max] )</h3>
+		<div>
+		[page:Float min] - the minimum value the length will be clamped to <br />
+		[page:Float max] - the maximum value the length will be clamped to<br /><br />
+
+		If this vector's length is greater than the max value, it is replaced by the max value. <br /><br />
+		If this vector's length is less than the min value, it is replaced by the min value.
+		</div>
+
 		<h3>[method:Vector4 clampScalar]( [page:Float min], [page:Float max] )</h3>
 		<h3>[method:Vector4 clampScalar]( [page:Float min], [page:Float max] )</h3>
 		<div>
 		<div>
 		[page:Float min] - the minimum value the components will be clamped to <br />
 		[page:Float min] - the minimum value the components will be clamped to <br />

+ 4 - 4
src/math/Vector2.js

@@ -257,7 +257,7 @@ Object.assign( Vector2.prototype, {
 
 
 	clamp: function ( min, max ) {
 	clamp: function ( min, max ) {
 
 
-		// This function assumes min < max, if this assumption isn't true it will not operate correctly
+		// assumes min < max, componentwise
 
 
 		this.x = Math.max( min.x, Math.min( max.x, this.x ) );
 		this.x = Math.max( min.x, Math.min( max.x, this.x ) );
 		this.y = Math.max( min.y, Math.min( max.y, this.y ) );
 		this.y = Math.max( min.y, Math.min( max.y, this.y ) );
@@ -286,7 +286,7 @@ Object.assign( Vector2.prototype, {
 
 
 		var length = this.length();
 		var length = this.length();
 
 
-		return this.multiplyScalar( Math.max( min, Math.min( max, length ) ) / length );
+		return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
 
 
 	},
 	},
 
 
@@ -361,7 +361,7 @@ Object.assign( Vector2.prototype, {
 
 
 	normalize: function () {
 	normalize: function () {
 
 
-		return this.divideScalar( this.length() );
+		return this.divideScalar( this.length() || 1 );
 
 
 	},
 	},
 
 
@@ -398,7 +398,7 @@ Object.assign( Vector2.prototype, {
 
 
 	setLength: function ( length ) {
 	setLength: function ( length ) {
 
 
-		return this.multiplyScalar( length / this.length() );
+		return this.normalize().multiplyScalar( length );
 
 
 	},
 	},
 
 

+ 4 - 4
src/math/Vector3.js

@@ -391,7 +391,7 @@ Object.assign( Vector3.prototype, {
 
 
 	clamp: function ( min, max ) {
 	clamp: function ( min, max ) {
 
 
-		// This function assumes min < max, if this assumption isn't true it will not operate correctly
+		// assumes min < max, componentwise
 
 
 		this.x = Math.max( min.x, Math.min( max.x, this.x ) );
 		this.x = Math.max( min.x, Math.min( max.x, this.x ) );
 		this.y = Math.max( min.y, Math.min( max.y, this.y ) );
 		this.y = Math.max( min.y, Math.min( max.y, this.y ) );
@@ -421,7 +421,7 @@ Object.assign( Vector3.prototype, {
 
 
 		var length = this.length();
 		var length = this.length();
 
 
-		return this.multiplyScalar( Math.max( min, Math.min( max, length ) ) / length );
+		return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
 
 
 	},
 	},
 
 
@@ -503,13 +503,13 @@ Object.assign( Vector3.prototype, {
 
 
 	normalize: function () {
 	normalize: function () {
 
 
-		return this.divideScalar( this.length() );
+		return this.divideScalar( this.length() || 1 );
 
 
 	},
 	},
 
 
 	setLength: function ( length ) {
 	setLength: function ( length ) {
 
 
-		return this.multiplyScalar( length / this.length() );
+		return this.normalize().multiplyScalar( length );
 
 
 	},
 	},
 
 

+ 19 - 5
src/math/Vector4.js

@@ -424,7 +424,7 @@ Object.assign( Vector4.prototype, {
 
 
 	clamp: function ( min, max ) {
 	clamp: function ( min, max ) {
 
 
-		// This function assumes min < max, if this assumption isn't true it will not operate correctly
+		// assumes min < max, componentwise
 
 
 		this.x = Math.max( min.x, Math.min( max.x, this.x ) );
 		this.x = Math.max( min.x, Math.min( max.x, this.x ) );
 		this.y = Math.max( min.y, Math.min( max.y, this.y ) );
 		this.y = Math.max( min.y, Math.min( max.y, this.y ) );
@@ -437,11 +437,17 @@ Object.assign( Vector4.prototype, {
 
 
 	clampScalar: function () {
 	clampScalar: function () {
 
 
-		var min = new Vector4();
-		var max = new Vector4();
+		var min, max;
 
 
 		return function clampScalar( minVal, maxVal ) {
 		return function clampScalar( minVal, maxVal ) {
 
 
+			if ( min === undefined ) {
+
+				min = new Vector4();
+				max = new Vector4();
+
+			}
+
 			min.set( minVal, minVal, minVal, minVal );
 			min.set( minVal, minVal, minVal, minVal );
 			max.set( maxVal, maxVal, maxVal, maxVal );
 			max.set( maxVal, maxVal, maxVal, maxVal );
 
 
@@ -451,6 +457,14 @@ Object.assign( Vector4.prototype, {
 
 
 	}(),
 	}(),
 
 
+	clampLength: function ( min, max ) {
+
+		var length = this.length();
+
+		return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
+
+	},
+
 	floor: function () {
 	floor: function () {
 
 
 		this.x = Math.floor( this.x );
 		this.x = Math.floor( this.x );
@@ -532,13 +546,13 @@ Object.assign( Vector4.prototype, {
 
 
 	normalize: function () {
 	normalize: function () {
 
 
-		return this.divideScalar( this.length() );
+		return this.divideScalar( this.length() || 1 );
 
 
 	},
 	},
 
 
 	setLength: function ( length ) {
 	setLength: function ( length ) {
 
 
-		return this.multiplyScalar( length / this.length() );
+		return this.normalize().multiplyScalar( length );
 
 
 	},
 	},