Browse Source

add Plane.flip() plus unit test.

Ben Houston 12 years ago
parent
commit
911146dba1
2 changed files with 21 additions and 0 deletions
  1. 9 0
      src/math/Plane.js
  2. 12 0
      test/unit/math/Plane.js

+ 9 - 0
src/math/Plane.js

@@ -74,6 +74,15 @@ THREE.Plane.prototype = {
 
 
 	},
 	},
 
 
+	flip: function () {
+
+		this.constant *= -1;
+		this.normal.negate();
+
+		return this;
+
+	},
+
 	distanceToPoint: function ( point ) {
 	distanceToPoint: function ( point ) {
 
 
 		return this.normal.dot( point ) + this.constant;
 		return this.normal.dot( point ) + this.constant;

+ 12 - 0
test/unit/math/Plane.js

@@ -94,6 +94,18 @@ test( "normalize", function() {
 	ok( a.constant == 1, "Passed!" );
 	ok( a.constant == 1, "Passed!" );
 });
 });
 
 
+test( "flip/distanceToPoint", function() {
+	var a = new THREE.Plane( new THREE.Vector3( 2, 0, 0 ), -2 );
+	
+	a.normalize();
+	ok( a.distanceToPoint( new THREE.Vector3( 4, 0, 0 ) ) === 3, "Passed!" );
+	ok( a.distanceToPoint( new THREE.Vector3( 1, 0, 0 ) ) === 0, "Passed!" );
+
+	a.flip();
+	ok( a.distanceToPoint( new THREE.Vector3( 4, 0, 0 ) ) === -3, "Passed!" );
+	ok( a.distanceToPoint( new THREE.Vector3( 1, 0, 0 ) ) === 0, "Passed!" );
+});
+
 test( "distanceToPoint", function() {
 test( "distanceToPoint", function() {
 	var a = new THREE.Plane( new THREE.Vector3( 2, 0, 0 ), -2 );
 	var a = new THREE.Plane( new THREE.Vector3( 2, 0, 0 ), -2 );