Browse Source

Vector*: Simplified clamp(). See #7326.

Mr.doob 9 years ago
parent
commit
9776d27144
3 changed files with 9 additions and 87 deletions
  1. 2 19
      src/math/Vector2.js
  2. 3 29
      src/math/Vector3.js
  3. 4 39
      src/math/Vector4.js

+ 2 - 19
src/math/Vector2.js

@@ -252,25 +252,8 @@ THREE.Vector2.prototype = {
 
 
 		// This function assumes min < max, if this assumption isn't true it will not operate correctly
 		// This function assumes min < max, if this assumption isn't true it will not operate correctly
 
 
-		if ( this.x < min.x ) {
-
-			this.x = min.x;
-
-		} else if ( this.x > max.x ) {
-
-			this.x = max.x;
-
-		}
-
-		if ( this.y < min.y ) {
-
-			this.y = min.y;
-
-		} else if ( this.y > max.y ) {
-
-			this.y = max.y;
-
-		}
+		this.x = Math.max( min.x, Math.min( max.x, this.x ) );
+		this.y = Math.max( min.y, Math.min( max.y, this.y ) );
 
 
 		return this;
 		return this;
 
 

+ 3 - 29
src/math/Vector3.js

@@ -464,35 +464,9 @@ THREE.Vector3.prototype = {
 
 
 		// This function assumes min < max, if this assumption isn't true it will not operate correctly
 		// This function assumes min < max, if this assumption isn't true it will not operate correctly
 
 
-		if ( this.x < min.x ) {
-
-			this.x = min.x;
-
-		} else if ( this.x > max.x ) {
-
-			this.x = max.x;
-
-		}
-
-		if ( this.y < min.y ) {
-
-			this.y = min.y;
-
-		} else if ( this.y > max.y ) {
-
-			this.y = max.y;
-
-		}
-
-		if ( this.z < min.z ) {
-
-			this.z = min.z;
-
-		} else if ( this.z > max.z ) {
-
-			this.z = max.z;
-
-		}
+		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.z = Math.max( min.z, Math.min( max.z, this.z ) );
 
 
 		return this;
 		return this;
 
 

+ 4 - 39
src/math/Vector4.js

@@ -473,45 +473,10 @@ THREE.Vector4.prototype = {
 
 
 		// This function assumes min < max, if this assumption isn't true it will not operate correctly
 		// This function assumes min < max, if this assumption isn't true it will not operate correctly
 
 
-		if ( this.x < min.x ) {
-
-			this.x = min.x;
-
-		} else if ( this.x > max.x ) {
-
-			this.x = max.x;
-
-		}
-
-		if ( this.y < min.y ) {
-
-			this.y = min.y;
-
-		} else if ( this.y > max.y ) {
-
-			this.y = max.y;
-
-		}
-
-		if ( this.z < min.z ) {
-
-			this.z = min.z;
-
-		} else if ( this.z > max.z ) {
-
-			this.z = max.z;
-
-		}
-
-		if ( this.w < min.w ) {
-
-			this.w = min.w;
-
-		} else if ( this.w > max.w ) {
-
-			this.w = max.w;
-
-		}
+		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.z = Math.max( min.z, Math.min( max.z, this.z ) );
+		this.w = Math.max( min.w, Math.min( max.w, this.w ) );
 
 
 		return this;
 		return this;