Advertisement
MrMusAddict

Expanding Shape Processing

Jan 24th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. ExpShape es;
  2.  
  3. void setup() {
  4.   size(600, 600);
  5.  
  6.   //make a new expanding shape
  7.   es = new ExpShape();
  8.  
  9.   //for 36 steps around a circle, you have a 60% chance to add a vertex, which can vary between 0 and width/2-100 in radius from the center
  10.   //just a fun way to makes some weird random shapes
  11.   for(int i = 0; i < 36; i++){
  12.     if(random(1) > 0.4){
  13.       float ranRad = random((width-200)/2.0);
  14.       es.vert(sin(TWO_PI/36.0 * i)*ranRad + width/2, cos(TWO_PI/36.0 * i)*ranRad + height/2);
  15.     }
  16.   }
  17. }
  18.  
  19. void draw() {
  20.   background(51);
  21.   stroke(color(255,0,0));
  22.   fill(255);
  23.   es.setThickness((cos(frameCount/32.0 + PI)+1)*50.0);
  24.   es.show();
  25. }
  26.  
  27. class ExpLine {
  28.   float thick;  //the thickness of the line
  29.   PVector p1;  //point 1 of the expanding line
  30.   PVector p2;  //point 2 of the expanding line
  31.   PShape s;  //the over-all shape of the expanded line, including the perimeter.
  32.  
  33.   ExpLine(float x1, float y1, float x2, float y2) {
  34.     p1 = new PVector(x1, y1);
  35.     p2 = new PVector(x2, y2);
  36.     thick = 50.0;
  37.   }
  38.  
  39.   void show() {
  40.     s = createShape();
  41.     s.beginShape();
  42.    
  43.     //make a half-circle of vertices around p1, distance "thick" appart
  44.     for (int i = 10; i < 28; i++) {
  45.       PVector rotA = new PVector(p2.x-p1.x, p2.y-p1.y).normalize(); //a rotator PVector pointing from p1 to p2.
  46.       rotA.rotate(TWO_PI / 36.0 * i); //rotate the vector between half-PI and three-halves-PI (i ranges from 10 to 28, so it's 10/36 to 28/36, multiplied by TWO_PI)
  47.       rotA.x *= abs(thick);
  48.       rotA.y *= abs(thick);
  49.       s.vertex(p1.x + rotA.x, p1.y + rotA.y);
  50.     }
  51.    
  52.     //make a half-circle of vertices around p2, distance "thick" appart
  53.     for (int i = 9; i < 28; i++) {
  54.       PVector rotB = new PVector(p1.x-p2.x, p1.y-p2.y).normalize(); //a rotator PVector pointing from p2 to p1.
  55.       rotB.rotate(TWO_PI / 36.0 * i); //rotate the vector between half-PI and three-halves-PI (i ranges from 10 to 28, so it's 10/36 to 28/36, multiplied by TWO_PI)
  56.       rotB.x *= abs(thick);
  57.       rotB.y *= abs(thick);
  58.       s.vertex(p2.x + rotB.x, p2.y + rotB.y);
  59.     }
  60.     s.endShape(CLOSE);
  61.     shape(s, 0, 0);
  62.   }
  63. }
  64.  
  65. class ExpShape {
  66.   ArrayList<PVector> verts; //array of vertices, so we can draw the under-lining shape
  67.   ArrayList<ExpLine> lines; //array of expanding lines, so that we can expand the perimeter of the shape
  68.   PShape s;
  69.  
  70.   ExpShape() {
  71.     //both array lists begin empty, and will only change once you add vertices manually, much like PShape.
  72.     verts = new ArrayList<PVector>();
  73.     lines = new ArrayList<ExpLine>();
  74.   }
  75.  
  76.   //manually add vertex, and recalculate shape
  77.   void vert(float x, float y) {
  78.     verts.add(new PVector(x, y));
  79.     updateShape();
  80.   }
  81.  
  82.   void show() {
  83.     shape(s, 0, 0);
  84.     for (ExpLine l : lines) {
  85.       l.show();
  86.     }
  87.   }
  88.  
  89.   void updateShape() {
  90.     lines.clear(); //clear all lines
  91.  
  92.     s = createShape();
  93.     s.beginShape();
  94.     for (int i = 0; i < verts.size(); i++) {
  95.      
  96.       //if we're on the last vertex of the shape, add an expanding line from the last point to the first
  97.       //otherwise, add a line from the current point to the next
  98.       if (i == verts.size()-1) {
  99.         lines.add(new ExpLine(verts.get(i).x, verts.get(i).y, verts.get(0).x, verts.get(0).y));
  100.       } else {
  101.         lines.add(new ExpLine(verts.get(i).x, verts.get(i).y, verts.get(i+1).x, verts.get(i+1).y));
  102.       }
  103.      
  104.       //add a vertex for the underlying shape
  105.       s.vertex(verts.get(i).x, verts.get(i).y);
  106.     }
  107.     s.endShape(CLOSE);
  108.   }
  109.  
  110.   //adjust the thickness for all ExpandingLines, in effect increasing the perimeter of the overall shape
  111.   void setThickness(float t) {
  112.     for (ExpLine l : lines) {
  113.       l.thick = t;
  114.     }
  115.   }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement