見出し画像

Generative Art #4

描き溜めた絵をちょっとずつ放出.今回はGenerative Art #3 の派生.白黒だけでなんかやってみたかった.Javaは関数をインスタンスみたいに使えないのが不便.

画像1

画像2

画像3

画像4

AbstractShape[] shapeList;

void setup() {
 size(800, 800);
 
 background(0);
 rectMode(CENTER);
 
 shapeList = new AbstractShape[] {
   (AbstractShape)(new RectShape()),
   (AbstractShape)(new CircleShape()),
   (AbstractShape)(new StripeShape()),
   (AbstractShape)(new PolkaDotShape()),
   (AbstractShape)(new DarkPolkaDotShape())
 };
 
 drawPattern();
}

void draw() {
 
}

void drawPattern() {
 float sf = random(1.5, 2.0);
 float rot = TWO_PI / (int)random(1, 13);
 
 translate(width / 2.0, height / 2.0);
 scale(sf, sf);
 rotate(rot);
 
 background(0);
 recur(new PVector(0.0, 0.0), width, 0);
}

void recur(PVector p, float s, int d) {
 if (d > 1 && random(1) < float(d) / 6) {
   AbstractShape shape = shapeList[(int)random(shapeList.length)];
   shape.drawShape(p, s, d);
 } else {
   recur(new PVector(p.x + s / 4.0, p.y + s / 4.0), s / 2.0, d + 1);
   recur(new PVector(p.x - s / 4.0, p.y + s / 4.0), s / 2.0, d + 1);
   recur(new PVector(p.x + s / 4.0, p.y - s / 4.0), s / 2.0, d + 1);
   recur(new PVector(p.x - s / 4.0, p.y - s / 4.0), s / 2.0, d + 1);
 }
}

static final String timestamp(final String name, final String ext) {
return name + "-" + year() + nf(month(), 2) + nf(day(), 2) +
  "-" + nf(hour(), 2) + nf(minute(), 2) + nf(second(), 2) + ext;
}

void mouseReleased() {
 drawPattern();
}

void keyReleased() {
 saveFrame(String.format("frames/%s", timestamp("Project", ".png")));
}

abstract class AbstractShape {
 abstract void drawShape(PVector p, float s, int d);
}

class RectShape extends AbstractShape{
 void drawShape(PVector p, float s, int d) {
   fill(0);
   stroke(255);
   
   int n = (int)random(1, 5);
   pushMatrix();
   translate(p.x, p.y);
   for (int k = 0; k < n; k++) {
     rect(0, 0, s, s);
     
     scale(1.0 / sqrt(2), 1.0 / sqrt(2)); // 回転によって外にはみ出ないようにする
     rotate(PI / 4);
     strokeWeight(pow(sqrt(2), k)); // scaleによって線が細くなり過ぎないようにする
   }
   popMatrix();
   
   strokeWeight(1.0);
 }
}

class CircleShape extends AbstractShape{
 void drawShape(PVector p, float s, int d) {
   fill(0);
   stroke(255);
   
   int n = (int)random(1, 6);
   for (int k = 0; k < n; k++) {
     circle(p.x, p.y, s - s / (float)n * k);
   }
 }
}

class StripeShape extends AbstractShape{
 void drawShape(PVector p, float s, int d) {
   
   boolean rotation = random(1) > 0.5;
   
   fill(255);
   noStroke();
   rect(p.x, p.y, s, s);
   blendMode(SUBTRACT);
   int n = (int)random(1, 6) * 2;
   for (int k = 0; k < n; k++) {
     if (k % 2 != 0) {
       if (!rotation) {
         rect(p.x - s / 2.0 + s / (float)n * k, p.y, s / (float)n, s);
       } else {
         rect(p.x, p.y - s / 2.0 + s / (float)n * k, s, s / (float)n);
       }
     }
   }
   blendMode(BLEND);
 }
}

class PolkaDotShape extends AbstractShape {
 void drawShape(PVector p, float s, int d) {
   fill(255);
   noStroke();
   
   rect(p.x, p.y, s, s);
   
   blendMode(SUBTRACT);
   int n = (int)random(1, 6) * 2 + 1;
   float r = s / (float)n;
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < n; j++) {
       if ((i * n + j) % 2 == 0) {
         float x = p.x - s / 2.0 + r / 2.0 + r * j;
         float y = p.y - s / 2.0 + r / 2.0 + r * i;
         circle(x, y, r / 2.0);
       }
     }
   }
   blendMode(BLEND);
 }
}

class DarkPolkaDotShape extends AbstractShape {
 void drawShape(PVector p, float s, int d) {
   fill(0);
   noStroke();
   
   rect(p.x, p.y, s, s);
   
   blendMode(ADD);
   fill(255);
   int n = (int)random(1, 6) * 2 + 1;
   float r = s / (float)n;
   for (int i = 0; i < n; i++) {
     for (int j = 0; j < n; j++) {
       if ((i * n + j) % 2 == 0) {
         float x = p.x - s / 2.0 + r / 2.0 + r * j;
         float y = p.y - s / 2.0 + r / 2.0 + r * i;
         circle(x, y, r / 2.0);
       }
     }
   }
   blendMode(BLEND);
 }
}

いただいたサポートは主にクリエイターとしての活動費になります