Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…ng new methods

new additions: containsPoly(), intersectsPoly(), getEdges(),
scale/rotate/translate()
  • Loading branch information...
commit ade83c99f2035bb94a11e89b6eec0339a64a95b6 1 parent ec80d65
karsten schmidt authored

Showing 1 changed file with 86 additions and 4 deletions. Show diff stats Hide diff stats

  1. +86 4 src/core/geom/Polygon2D.js
90 src/core/geom/Polygon2D.js
... ... @@ -1,9 +1,9 @@
1 1 toxi.Polygon2D = function(){
2 2 this.vertices = [];
3 3 if(arguments.length > 1){ //comma-separated Vec2D's were passed in
4   - var l = agruments.length;
  4 + var l = arguments.length;
5 5 for(var i=0;i<l;i++){
6   - this.add(p.copy());
  6 + this.add(arguments[i].copy());
7 7 }
8 8 } else if(arguments.length == 1){
9 9 var arg = arguments[0];
@@ -29,7 +29,7 @@ toxi.Polygon2D.prototype = {
29 29 containsPoint: function(p){
30 30 var num = this.vertices.length;
31 31 var i,j = num-1;
32   - var addNodes = false;
  32 + var oddNodes = false;
33 33 var px = p.x;
34 34 var py = p.y;
35 35 for(i=0;i<num;i++){
@@ -42,9 +42,18 @@ toxi.Polygon2D.prototype = {
42 42 }
43 43 j = i;
44 44 }
45   - return addNodes;
  45 + return oddNodes;
46 46 },
47 47
  48 + containsPoly: function(poly) {
  49 + for (var i=0,num=poly.vertices.length; i<num; i++) {
  50 + if (!this.containsPoint(poly.vertices[i])) {
  51 + return false;
  52 + }
  53 + }
  54 + return true;
  55 + },
  56 +
48 57 flipVertexOrder: function(){
49 58 this.vertices.reverse();
50 59 return this;
@@ -84,6 +93,15 @@ toxi.Polygon2D.prototype = {
84 93 return circ;
85 94 },
86 95
  96 + getEdges: function() {
  97 + var num = this.vertices.length;
  98 + var edges = new Array(num);
  99 + for (var i = 0; i < num; i++) {
  100 + edges[i]=new toxi.Line2D(this.vertices[i], this.vertices[(i + 1) % num]);
  101 + }
  102 + return edges;
  103 + },
  104 +
87 105 getNumPoints: function(){
88 106 return this.vertices.length;
89 107 },
@@ -95,6 +113,70 @@ toxi.Polygon2D.prototype = {
95 113 return false;
96 114 },
97 115
  116 + intersectsPoly: function(poly) {
  117 + if (!this.containsPoly(poly)) {
  118 + var edges=this.getEdges();
  119 + var pedges=poly.getEdges();
  120 + for(var i=0, n=edges.length; i < n; i++) {
  121 + for(var j=0, m = pedges.length, e = edges[i]; j < m; j++) {
  122 + if (e.intersectLine(pedges[j]).getType() == toxi.Line2D.LineIntersection.Type.INTERSECTING) {
  123 + return true;
  124 + }
  125 + }
  126 + }
  127 + return false;
  128 + } else {
  129 + return true;
  130 + }
  131 + },
  132 +
  133 + rotate: function(theta) {
  134 + for (var i=0, num=this.vertices.length; i < num; i++) {
  135 + this.vertices[i].rotate(theta);
  136 + }
  137 + },
  138 +
  139 + scale: function() {
  140 + var x,y;
  141 + if (arguments.length==1) {
  142 + var arg = arguments[0];
  143 + if(arg instanceof toxi.Vec2D){
  144 + x=arg.x;
  145 + y=arg.y;
  146 + } else {
  147 + // uniform scale
  148 + x=arg;
  149 + y=arg;
  150 + }
  151 + } else if (arguments.length==2) {
  152 + x=arguments[0];
  153 + y=arguments[1];
  154 + } else {
  155 + throw "Invalid argument(s) passed.";
  156 + }
  157 + for (var i=0, num=this.vertices.length; i < num; i++) {
  158 + this.vertices[i].scaleSelf(x, y);
  159 + }
  160 + return this;
  161 + },
  162 +
  163 + translate: function() {
  164 + var x,y;
  165 + if (arguments.length==1 && arguments[0] instanceof toxi.Vec2D){
  166 + x=arg.x;
  167 + y=arg.y;
  168 + } else if (arguments.length==2) {
  169 + x=arguments[0];
  170 + y=arguments[1];
  171 + } else {
  172 + throw "Invalid argument(s) passed.";
  173 + }
  174 + for (var i=0, num=this.vertices.length; i < num; i++) {
  175 + this.vertices[i].addSelf(x, y);
  176 + }
  177 + return this;
  178 + },
  179 +
98 180 smooth: function(amount, baseWeight){
99 181 var centroid = this.getCentroid();
100 182 var num = this.vertices.length;

0 comments on commit ade83c9

Please sign in to comment.
Something went wrong with that request. Please try again.