Permalink
Browse files

fixing various bug in Polygon2D (constructor & containsPoint()), addi…

…ng new methods

new additions: containsPoly(), intersectsPoly(), getEdges(),
scale/rotate/translate()
  • Loading branch information...
1 parent ec80d65 commit ade83c99f2035bb94a11e89b6eec0339a64a95b6 karsten schmidt committed Sep 7, 2011
Showing with 86 additions and 4 deletions.
  1. +86 −4 src/core/geom/Polygon2D.js
View
90 src/core/geom/Polygon2D.js
@@ -1,9 +1,9 @@
toxi.Polygon2D = function(){
this.vertices = [];
if(arguments.length > 1){ //comma-separated Vec2D's were passed in
- var l = agruments.length;
+ var l = arguments.length;
for(var i=0;i<l;i++){
- this.add(p.copy());
+ this.add(arguments[i].copy());
}
} else if(arguments.length == 1){
var arg = arguments[0];
@@ -29,7 +29,7 @@ toxi.Polygon2D.prototype = {
containsPoint: function(p){
var num = this.vertices.length;
var i,j = num-1;
- var addNodes = false;
+ var oddNodes = false;
var px = p.x;
var py = p.y;
for(i=0;i<num;i++){
@@ -42,9 +42,18 @@ toxi.Polygon2D.prototype = {
}
j = i;
}
- return addNodes;
+ return oddNodes;
},
+ containsPoly: function(poly) {
+ for (var i=0,num=poly.vertices.length; i<num; i++) {
+ if (!this.containsPoint(poly.vertices[i])) {
+ return false;
+ }
+ }
+ return true;
+ },
+
flipVertexOrder: function(){
this.vertices.reverse();
return this;
@@ -84,6 +93,15 @@ toxi.Polygon2D.prototype = {
return circ;
},
+ getEdges: function() {
+ var num = this.vertices.length;
+ var edges = new Array(num);
+ for (var i = 0; i < num; i++) {
+ edges[i]=new toxi.Line2D(this.vertices[i], this.vertices[(i + 1) % num]);
+ }
+ return edges;
+ },
+
getNumPoints: function(){
return this.vertices.length;
},
@@ -95,6 +113,70 @@ toxi.Polygon2D.prototype = {
return false;
},
+ intersectsPoly: function(poly) {
+ if (!this.containsPoly(poly)) {
+ var edges=this.getEdges();
+ var pedges=poly.getEdges();
+ for(var i=0, n=edges.length; i < n; i++) {
+ for(var j=0, m = pedges.length, e = edges[i]; j < m; j++) {
+ if (e.intersectLine(pedges[j]).getType() == toxi.Line2D.LineIntersection.Type.INTERSECTING) {
+ return true;
+ }
+ }
+ }
+ return false;
+ } else {
+ return true;
+ }
+ },
+
+ rotate: function(theta) {
+ for (var i=0, num=this.vertices.length; i < num; i++) {
+ this.vertices[i].rotate(theta);
+ }
+ },
+
+ scale: function() {
+ var x,y;
+ if (arguments.length==1) {
+ var arg = arguments[0];
+ if(arg instanceof toxi.Vec2D){
+ x=arg.x;
+ y=arg.y;
+ } else {
+ // uniform scale
+ x=arg;
+ y=arg;
+ }
+ } else if (arguments.length==2) {
+ x=arguments[0];
+ y=arguments[1];
+ } else {
+ throw "Invalid argument(s) passed.";
+ }
+ for (var i=0, num=this.vertices.length; i < num; i++) {
+ this.vertices[i].scaleSelf(x, y);
+ }
+ return this;
+ },
+
+ translate: function() {
+ var x,y;
+ if (arguments.length==1 && arguments[0] instanceof toxi.Vec2D){
+ x=arg.x;
+ y=arg.y;
+ } else if (arguments.length==2) {
+ x=arguments[0];
+ y=arguments[1];
+ } else {
+ throw "Invalid argument(s) passed.";
+ }
+ for (var i=0, num=this.vertices.length; i < num; i++) {
+ this.vertices[i].addSelf(x, y);
+ }
+ return this;
+ },
+
smooth: function(amount, baseWeight){
var centroid = this.getCentroid();
var num = this.vertices.length;

0 comments on commit ade83c9

Please sign in to comment.