Browse Source

added centroid to path

zz85 14 years ago
parent
commit
f875cfc6ad
1 changed files with 12 additions and 21 deletions
  1. 12 21
      src/extras/geometries/Path.js

+ 12 - 21
src/extras/geometries/Path.js

@@ -149,21 +149,6 @@ THREE.Path.prototype.arc = function ( aX, aY, aRadius,
 
 
  };
  };
 
 
-/*
-// FUTURE ENHANCEMENTS
-
- example usage?
-
- Path.addExprFunc('sineCurveTo', sineCurveGetPtFunction)
- Path.sineCurveTo(x,y, amptitude);
- sineCurve.getPoint(t);
- return sine(disnt) * ampt
-
- // Create a new func eg. sin (theta) x
- THREE.Path.prototype.addExprFunc = function(exprName, func) {
- };
-*/
-
 
 
 THREE.Path.prototype.getSpacedPoints = function ( divisions, closedPath ) {
 THREE.Path.prototype.getSpacedPoints = function ( divisions, closedPath ) {
 
 
@@ -384,7 +369,10 @@ THREE.Path.prototype.getPoints = function( divisions, closedPath ) {
 
 
 };
 };
 
 
-THREE.Path.prototype.getMinAndMax = function () {
+
+// Returns min and max coordinates, as well as centroid
+
+THREE.Path.prototype.getBoundingBox = function () {
 
 
 	var points = this.getPoints();
 	var points = this.getPoints();
 
 
@@ -394,7 +382,9 @@ THREE.Path.prototype.getMinAndMax = function () {
 	maxX = maxY = Number.NEGATIVE_INFINITY;
 	maxX = maxY = Number.NEGATIVE_INFINITY;
 	minX = minY = Number.POSITIVE_INFINITY;
 	minX = minY = Number.POSITIVE_INFINITY;
 
 
-	var p, i, il;
+	var p, i, il, sum;
+
+	sum = new THREE.Vector2();
 
 
 	for ( i = 0, il = points.length; i < il; i ++ ) {
 	for ( i = 0, il = points.length; i < il; i ++ ) {
 
 
@@ -406,16 +396,17 @@ THREE.Path.prototype.getMinAndMax = function () {
 		if ( p.y > maxY ) maxY = p.y;
 		if ( p.y > maxY ) maxY = p.y;
 		else if ( p.y < maxY ) minY = p.y;
 		else if ( p.y < maxY ) minY = p.y;
 
 
+		sum.addSelf(p.x, p.y);
+		
 	}
 	}
 
 
-	// TODO Include CG or find mid-pt?
-
 	return {
 	return {
 
 
 		minX: minX,
 		minX: minX,
 		minY: minY,
 		minY: minY,
 		maxX: maxX,
 		maxX: maxX,
-		maxY: maxY
+		maxY: maxY,
+		centroid: sum.divideScalar(il)
 
 
 	};
 	};
 
 
@@ -534,7 +525,7 @@ THREE.Path.prototype.transform = function( path ) {
 
 
 	console.log( path.cacheArcLengths() );
 	console.log( path.cacheArcLengths() );
 
 
-	var thisBounds = this.getMinAndMax();
+	var thisBounds = this.getBoundingBox();
 	var oldPts = this.getPoints();
 	var oldPts = this.getPoints();
 
 
 	var i, il, p, oldX, oldY, xNorm;
 	var i, il, p, oldX, oldY, xNorm;