日常の進捗

主に自分のための,行為とその習慣化の記録

Mod: Generative Design / P_2_1_2_04

f:id:takawo:20180111073239g:plain

タイリングした矩形の頂点をランダムにバラけさせる.randomSeed()の使い方(引数の値が固定されるとフレームごとのrandomの値が同じ=固定される)はこれまでもやってきているけど,こういったシミュレーションのスケッチだと便利に使える.

コード

import processing.pdf.*;
import java.util.Calendar;

boolean savePDF = false;


int tileCount = 20;
int rectWidth;
int rectHeight;

int actRandomSeed = (int) random(100000);

color c1;
color c2;
color c3;
color c4;

void setup() {
  size(800, 800, P2D);
  blendMode(ADD);
  colorMode(HSB, 360, 100, 100, 100);
  randomSeed(actRandomSeed);
  initColor();
  noStroke();
  rectWidth = width / tileCount;
  rectHeight = height / tileCount;
}

void draw() {
  if (savePDF) {
    beginRecord(PDF, timestamp() + ".pdf");
  }
  background(c1);
  randomSeed(actRandomSeed);
  int i =0;
  for (int gridY=0; gridY<tileCount; gridY++) {
    for (int gridX=0; gridX<tileCount; gridX++) {

      int posX = width/tileCount * gridX;
      int posY = height/tileCount * gridY;

      float shiftX1 = mouseX/20 * random(-1, 1);
      float shiftY1 = mouseY/20 * random(-1, 1);
      float shiftX2 = mouseX/20 * random(-1, 1);
      float shiftY2 = mouseY/20 * random(-1, 1);
      float shiftX3 = mouseX/20 * random(-1, 1);
      float shiftY3 = mouseY/20 * random(-1, 1);
      float shiftX4 = mouseX/20 * random(-1, 1);
      float shiftY4 = mouseY/20 * random(-1, 1);

      if (i%3 == 0) fill(c2);
      else if (i%3 == 1) fill(c3);
      else if (i%3 == 2) fill(c4);

      beginShape();
      vertex(posX+shiftX1, posY+shiftY1);
      vertex(posX+rectWidth+shiftX2, posY+shiftY2);
      vertex(posX+rectWidth+shiftX3, posY+rectHeight+shiftY3);
      vertex(posX+shiftX4, posY+rectHeight+shiftY4);
      endShape(CLOSE);
      i++;
    }
  }


  if (savePDF) {
    endRecord();
    savePDF = false;
  }
}

void mousePressed() {
  actRandomSeed = (int) random(100000);
  initColor();
}

void initColor() {
  float hue = random(360);
  float sat = random(50, 80);
  float bri = random(70, 100);
  float trans = random(30, 50);
  c1 = color(hue, sat, bri, 20);
  c2 = color((hue + 90)%360, sat, bri, trans);
  c3 = color((hue + 180)%360, sat, bri, trans);
  c4 = color((hue + 270)%360, sat, bri, trans);
}

void keyReleased() {
  if (key == 's' || key == 'S') saveFrame(timestamp()+"_##.png");
  if (key == 'p' || key == 'P') savePDF = true;
}

String timestamp() {
  Calendar now = Calendar.getInstance();
  return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}

リファレンス