Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float THRES = 0.5;
- float squareSize = 30;
- float noiseScale = 0.1;
- int w, h;
- void setup() {
- size(600, 1000);
- w = int(width/squareSize);
- h = int(height/squareSize);
- }
- void draw() {
- background(20);
- translate(width/2, height/2 + squareSize/2);
- strokeWeight(2);
- strokeCap(ROUND);
- strokeJoin(ROUND);
- blendMode(LIGHTEST);
- noiseScale = map(cos(millis()*0.0003), -1, 1, 0.005, 0.02);
- THRES = map(cos(millis()*0.0001), -1, 1, 0.3, 0.7);
- for (int x = -w/2; x < w/2; x++) {
- for (int y = -h/2; y < h/2; y++) {
- Square sq = new Square(new PVector(x * squareSize, y * squareSize), squareSize, noiseScale, 0);
- sq.drawLines(#FF0000);
- sq = new Square(new PVector(x * squareSize, y * squareSize), squareSize, noiseScale, -5);
- sq.drawLines(#0000FF);
- sq = new Square(new PVector(x * squareSize, y * squareSize), squareSize, noiseScale, 5);
- sq.drawLines(#00FF00);
- }
- }
- }
- class Square {
- byte state;
- float size;
- PVector center;
- float zOff;
- Square(PVector center, float size, float noiseScale, float zOff) {
- this.center = center;
- this.size = size;
- this.zOff = zOff;
- PVector p0 = getPoint0();
- PVector p1 = getPoint1();
- PVector p2 = getPoint2();
- PVector p3 = getPoint3();
- boolean a = getNoise(p0.x, p0.y, noiseScale) > THRES ? true : false;
- boolean b = getNoise(p1.x, p1.y, noiseScale) > THRES ? true : false;
- boolean c = getNoise(p2.x, p2.y, noiseScale) > THRES ? true : false;
- boolean d = getNoise(p3.x, p3.y, noiseScale) > THRES ? true : false;
- calculateState(a, b, c, d);
- }
- PVector getPoint0() {
- return new PVector(center.x - size/2.0, center.y + size/2.0);
- }
- PVector getPoint1() {
- return new PVector(center.x + size/2.0, center.y + size/2.0);
- }
- PVector getPoint2() {
- return new PVector(center.x + size/2.0, center.y - size/2.0);
- }
- PVector getPoint3() {
- return new PVector(center.x - size/2.0, center.y - size/2.0);
- }
- void calculateState(boolean a, boolean b, boolean c, boolean d) {
- byte bA = byte(a ? 1 : 0);
- byte bB = byte(b ? 1 : 0);
- byte bC = byte(c ? 1 : 0);
- byte bD = byte(d ? 1 : 0);
- state = byte(bA | bB << 1 | bC << 2 | bD << 3);
- }
- float getNoise(float x, float y, float scale) {
- return noise(x * scale, y * scale, millis()*0.0001 + zOff);
- }
- void drawLines(int lineColor) {
- float l = center.x-size/2.0;
- float r = center.x+size/2.0;
- float t = center.y-size/2.0;
- float b = center.y+size/2.0;
- stroke(lineColor);
- if (state == 1 || state == 14) {
- line(l, center.y, center.x, b);
- } else if (state == 2 || state == 13) {
- line(center.x, b, r, center.y);
- } else if (state == 3 || state == 12) {
- line(l, center.y, r, center.y);
- } else if (state == 4 || state == 11) {
- line(center.x, t, r, center.y);
- } else if (state == 5) {
- line(l, center.y, center.x, t);
- line(center.x, b, l, center.y);
- } else if (state == 6 || state == 9) {
- line(center.x, b, center.x, t);
- } else if (state == 7 || state == 8) {
- line(l, center.y, center.x, t);
- } else if (state == 10) {
- line(l, center.y, center.x, b);
- line(center.x, t, r, center.y);
- }
- }
- }
Add Comment
Please, Sign In to add comment