itblanco

marchingSquares

Nov 25th, 2020 (edited)
367
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. float THRES = 0.5;
  2.  
  3. float squareSize = 30;
  4. float noiseScale = 0.1;
  5. int w, h;
  6.  
  7. void setup() {
  8.   size(600, 1000);
  9.   w = int(width/squareSize);
  10.   h = int(height/squareSize);  
  11. }
  12.  
  13. void draw() {
  14.   background(20);
  15.   translate(width/2, height/2 + squareSize/2);
  16.   strokeWeight(2);
  17.   strokeCap(ROUND);
  18.   strokeJoin(ROUND);
  19.   blendMode(LIGHTEST);
  20.   noiseScale = map(cos(millis()*0.0003), -1, 1, 0.005, 0.02);
  21.   THRES = map(cos(millis()*0.0001), -1, 1, 0.3, 0.7);
  22.   for (int x = -w/2; x < w/2; x++) {
  23.     for (int y = -h/2; y < h/2; y++) {
  24.       Square sq = new Square(new PVector(x * squareSize, y * squareSize), squareSize, noiseScale, 0);
  25.       sq.drawLines(#FF0000);
  26.       sq = new Square(new PVector(x * squareSize, y * squareSize), squareSize, noiseScale, -5);
  27.       sq.drawLines(#0000FF);
  28.       sq = new Square(new PVector(x * squareSize, y * squareSize), squareSize, noiseScale, 5);
  29.       sq.drawLines(#00FF00);
  30.     }
  31.   }
  32. }
  33.  
  34. class Square {
  35.   byte state;
  36.   float size;
  37.   PVector center;
  38.   float zOff;
  39.  
  40.   Square(PVector center, float size, float noiseScale, float zOff) {
  41.     this.center = center;
  42.     this.size = size;
  43.     this.zOff = zOff;
  44.  
  45.     PVector p0 = getPoint0();
  46.     PVector p1 = getPoint1();
  47.     PVector p2 = getPoint2();
  48.     PVector p3 = getPoint3();
  49.  
  50.     boolean a = getNoise(p0.x, p0.y, noiseScale) > THRES ? true : false;
  51.     boolean b = getNoise(p1.x, p1.y, noiseScale) > THRES ? true : false;
  52.     boolean c = getNoise(p2.x, p2.y, noiseScale) > THRES ? true : false;
  53.     boolean d = getNoise(p3.x, p3.y, noiseScale) > THRES ? true : false;
  54.  
  55.     calculateState(a, b, c, d);
  56.   }
  57.  
  58.   PVector getPoint0() {
  59.     return new PVector(center.x - size/2.0, center.y + size/2.0);
  60.   }
  61.  
  62.   PVector getPoint1() {
  63.     return new PVector(center.x + size/2.0, center.y + size/2.0);
  64.   }
  65.  
  66.   PVector getPoint2() {
  67.     return new PVector(center.x + size/2.0, center.y - size/2.0);
  68.   }
  69.  
  70.   PVector getPoint3() {
  71.     return new PVector(center.x - size/2.0, center.y - size/2.0);
  72.   }
  73.  
  74.   void calculateState(boolean a, boolean b, boolean c, boolean d) {
  75.     byte bA = byte(a ? 1 : 0);
  76.     byte bB = byte(b ? 1 : 0);
  77.     byte bC = byte(c ? 1 : 0);
  78.     byte bD = byte(d ? 1 : 0);
  79.  
  80.     state = byte(bA | bB << 1 | bC << 2 | bD << 3);
  81.   }
  82.  
  83.   float getNoise(float x, float y, float scale) {
  84.     return noise(x * scale, y * scale, millis()*0.0001 + zOff);
  85.   }
  86.  
  87.   void drawLines(int lineColor) {
  88.     float l = center.x-size/2.0;
  89.     float r = center.x+size/2.0;
  90.     float t = center.y-size/2.0;
  91.     float b = center.y+size/2.0;
  92.     stroke(lineColor);    
  93.     if (state == 1 || state == 14) {
  94.       line(l, center.y, center.x, b);
  95.     } else if (state == 2 || state == 13) {
  96.       line(center.x, b, r, center.y);
  97.     } else if (state == 3 || state == 12) {
  98.       line(l, center.y, r, center.y);
  99.     } else if (state == 4 || state == 11) {
  100.       line(center.x, t, r, center.y);
  101.     } else if (state == 5) {
  102.       line(l, center.y, center.x, t);
  103.       line(center.x, b, l, center.y);
  104.     } else if (state == 6 || state == 9) {
  105.       line(center.x, b, center.x, t);
  106.     } else if (state == 7 || state == 8) {
  107.       line(l, center.y, center.x, t);
  108.     } else if (state == 10) {
  109.       line(l, center.y, center.x, b);
  110.       line(center.x, t, r, center.y);
  111.     }
  112.   }
  113. }
RAW Paste Data