Guest User

Poly.js

a guest
Dec 9th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Poly()
  2. {
  3.     this.verts = [];
  4.     this.pos = createVector(0, 0);
  5.     this.col = color(255);
  6.     this.colStroke = color(0);
  7.     this.useFill = true;
  8.     this.useStroke = false;
  9.     this.weightStroke = 1;
  10.     this.rot = 0;
  11.     this.scl = createVector(1, 1);
  12.     this.cnt = createVector(0, 0);
  13.    
  14.     this.draw = function()
  15.     {
  16.         if (this.verts.length == 0)
  17.             return;
  18.        
  19.         var b = this.getBounds();
  20.         var w = b.size.x;
  21.         var h = b.size.y;
  22.        
  23.         push();
  24.         translate(this.pos.x, this.pos.y);
  25.         rotate   (this.rot);
  26.         scale    (this.scl.x, this.scl.y);
  27.         translate(w * -this.cnt.x, h * -this.cnt.y);
  28.        
  29.         if (this.useFill)
  30.             fill(this.col);
  31.         else
  32.             noFill();
  33.  
  34.         if (this.useStroke)
  35.         {
  36.             strokeWeight(this.weightStroke);
  37.             stroke(this.colStroke);
  38.         }
  39.         else
  40.             noStroke();
  41.        
  42.         beginShape();
  43.         for (var i = 0; i < this.verts.length; ++ i)
  44.             vertex(this.verts[i].x, this.verts[i].y);
  45.         endShape(CLOSE);
  46.         pop();
  47.     };
  48.    
  49.     this.getBounds = function()
  50.     {
  51.         var r = {pos: createVector(0, 0), size: createVector(0, 0)};
  52.  
  53.         if (this.verts.length == 0)
  54.             return r;
  55.  
  56.         var xmin = this.verts[0].x;
  57.         var ymin = this.verts[0].y;
  58.         var xmax = this.verts[0].x;
  59.         var ymax = this.verts[0].y;
  60.  
  61.         for (var i = 0; i < this.verts.length; ++ i)
  62.         {
  63.             if (this.verts[i].x < xmin) xmin = this.verts[i].x;
  64.             if (this.verts[i].y < ymin) ymin = this.verts[i].y;
  65.             if (this.verts[i].x > xmax) xmax = this.verts[i].x;
  66.             if (this.verts[i].y > ymax) ymax = this.verts[i].y;
  67.         }
  68.        
  69.         r.pos.x  = xmin;
  70.         r.pos.y  = ymin;
  71.         r.size.x = xmax - xmin;
  72.         r.size.y = ymax - ymin;
  73.        
  74.         return r;
  75.     };
  76. }
Add Comment
Please, Sign In to add comment