Browse Source

Simplify Box2 and Box3 setFromPoints, add tests.

Jan Wrobel 11 years ago
parent
commit
d464040d5f
4 changed files with 36 additions and 48 deletions
  1. 3 34
      src/math/Box2.js
  2. 3 14
      src/math/Box3.js
  3. 15 0
      test/unit/math/Box2.js
  4. 15 0
      test/unit/math/Box3.js

+ 3 - 34
src/math/Box2.js

@@ -24,42 +24,11 @@ THREE.Box2.prototype = {
 
 	setFromPoints: function ( points ) {
 
-		if ( points.length > 0 ) {
+		this.makeEmpty();
 
-			var point = points[ 0 ];
+		for ( var i = 0, il = points.length; i < il; i ++ ) {
 
-			this.min.copy( point );
-			this.max.copy( point );
-
-			for ( var i = 1, il = points.length; i < il; i ++ ) {
-
-				point = points[ i ];
-
-				if ( point.x < this.min.x ) {
-
-					this.min.x = point.x;
-
-				} else if ( point.x > this.max.x ) {
-
-					this.max.x = point.x;
-
-				}
-
-				if ( point.y < this.min.y ) {
-
-					this.min.y = point.y;
-
-				} else if ( point.y > this.max.y ) {
-
-					this.max.y = point.y;
-
-				}
-
-			}
-
-		} else {
-
-			this.makeEmpty();
+			this.expandByPoint( points[ i ] )
 
 		}
 

+ 3 - 14
src/math/Box3.js

@@ -25,22 +25,11 @@ THREE.Box3.prototype = {
 
 	setFromPoints: function ( points ) {
 
-		if ( points.length > 0 ) {
+		this.makeEmpty();
 
-			var point = points[ 0 ];
+		for ( var i = 0, il = points.length; i < il; i ++ ) {
 
-			this.min.copy( point );
-			this.max.copy( point );
-
-			for ( var i = 1, il = points.length; i < il; i ++ ) {
-
-				this.expandByPoint( points[ i ] )
-
-			}
-
-		} else {
-
-			this.makeEmpty();
+			this.expandByPoint( points[ i ] )
 
 		}
 

+ 15 - 0
test/unit/math/Box2.js

@@ -39,6 +39,21 @@ test( "set", function() {
 	ok( a.max.equals( one2 ), "Passed!" );
 });
 
+test( "setFromPoints", function() {
+	var a = new THREE.Box2();
+
+	a.setFromPoints( [ zero2, one2, two2 ] );
+	ok( a.min.equals( zero2 ), "Passed!" );
+	ok( a.max.equals( two2 ), "Passed!" );
+
+	a.setFromPoints( [ one2 ] );
+	ok( a.min.equals( one2 ), "Passed!" );
+	ok( a.max.equals( one2 ), "Passed!" );
+
+	a.setFromPoints( [] );
+	ok( a.empty(), "Passed!" );
+});
+
 test( "empty/makeEmpty", function() {
 	var a = new THREE.Box2();
 

+ 15 - 0
test/unit/math/Box3.js

@@ -39,6 +39,21 @@ test( "set", function() {
 	ok( a.max.equals( one3 ), "Passed!" );
 });
 
+test( "setFromPoints", function() {
+	var a = new THREE.Box3();
+
+	a.setFromPoints( [ zero3, one3, two3 ] );
+	ok( a.min.equals( zero3 ), "Passed!" );
+	ok( a.max.equals( two3 ), "Passed!" );
+
+	a.setFromPoints( [ one3 ] );
+	ok( a.min.equals( one3 ), "Passed!" );
+	ok( a.max.equals( one3 ), "Passed!" );
+
+	a.setFromPoints( [] );
+	ok( a.empty(), "Passed!" );
+});
+
 test( "empty/makeEmpty", function() {
 	var a = new THREE.Box3();