Browse Source

simplify Sphere.js via use of Vector3.distanceTo*(), minimize code in Plane.js

Ben Houston 12 years ago
parent
commit
b6451318d7
2 changed files with 9 additions and 23 deletions
  1. 3 9
      src/core/Plane.js
  2. 6 14
      src/core/Sphere.js

+ 3 - 9
src/core/Plane.js

@@ -63,14 +63,8 @@
 	};
 
 	THREE.Plane.prototype.projectPoint = function ( point ) {		
-
-		var perpendicularMagnitude = this.distanceToPoint( point );
-
-		return new THREE.Vector3( 
-			point.x - this.normal.x * perpendicularMagnitude,
-			point.y - this.normal.y * perpendicularMagnitude,
-			point.z - this.normal.z * perpendicularMagnitude
-			);
+		
+		return new THREE.Vector3().copy( point ).sub( this.orthoPoint( point ) );
 	};
 
 	THREE.Plane.prototype.orthoPoint = function ( point ) {		
@@ -91,7 +85,7 @@
 
 	THREE.Plane.prototype.coplanarPoint = function () {		
 		
-		return this.projectPoint( new THREE.Vector3( 0, 0, 0 ) );
+		return this.projectPoint( new THREE.Vector3() );
 	};
 
 }( THREE ) );

+ 6 - 14
src/core/Sphere.js

@@ -38,36 +38,28 @@
 	THREE.Sphere.prototype.volume = function () {
 
 		return Math.PI * 4 / 3 * this.radius * this.radius * this.radius;
-
 	};
 
 	THREE.Sphere.prototype.containsPoint = function ( point ) {
 
-		var distanceSq = new THREE.VEctor3().sub( point, this.center ).lengthSq();
-
-		return ( distanceSq <= this.radius * this.radius );
+		return ( point.distanceToSquared( this.center ) <= this.radius * this.radius );
 	};
 
 	THREE.Sphere.prototype.distanceToPoint = function ( point ) {
 
-		var distanceSq = new THREE.Vector3().sub( point, this.center ).length();
-
-		return ( distanceSq - this.radius );
+		return ( point.distanceTo( this.center ) - this.radius );
 	};
 
 	THREE.Sphere.prototype.clampPoint = function ( point ) {
 
-		// NOTE: There is likely a more optimal way of doing this.
-
-		var delta = new THREE.Vector3().sub( point, this.center );
-
-		var deltaLengthSq = delta.lengthSq();
+		var deltaLengthSq = this.center.distanceToSquared( point );
 
 		if( deltaLengthSq > ( this.radius*this.radius ) ) {
 
-			delta.normalize().multiplyByScalar( this.radius ).addSelf( this.center );
-			return delta;
+			var delta = new THREE.Vector3().sub( point, center ).normalize();
+			delta.multiplyByScalar( this.radius ).addSelf( this.center );
 
+			return delta;
 		}
 
 		return point;