日常の進捗

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

Mod: Generative Design / P_2_1_2_02

f:id:takawo:20180109183048g:plain

84行目あたりのmoduleRadiusBackground = max(moduleRadiusBackground-2, 10);みたいな書き方はスマートでいいなと思った.

コード

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

boolean savePDF = false;

color moduleColorBackground;
color moduleColorForeground;

color moduleAlphaBackground;
color moduleAlphaForeground;

float moduleRadiusBackground = 30;
float moduleRadiusForeground = 15;
color background;

float tileCount = 20;
int actRandomSeed = (int) random(10000);

void setup() {
  size(960, 540);
  colorMode(HSB, 360, 100, 100, 100);
  initVariables();
}

void initVariables() {
  float hue = random(360);
  moduleColorBackground = color(hue, 80, 100);
  moduleColorForeground = color((hue+180)%360, 80, 100);
  int transparent = (int)random(50, 100);
  moduleAlphaBackground = transparent;
  moduleAlphaForeground = 100-transparent;
}

void draw() {
  if (savePDF) {
    beginRecord(PDF, timestamp()+".pdf");
    colorMode(HSB, 360, 100, 100, 100);
  }
  randomSeed(actRandomSeed);

  background = color(frameCount%360, 40, 40);
  translate(width / tileCount / 2, height / tileCount / 2);
  background(background);
  noStroke();

  for (int gridY=0; gridY<tileCount; gridY++) {
    for (int gridX=0; gridX<tileCount; gridX++) {

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

      float shiftX =  random(-2, 2) * mouseX/10;
      float shiftY =  random(-2, 2) * mouseY/10;

      fill(moduleColorBackground, moduleAlphaBackground);
      ellipse(posX+shiftX, posY+shiftY, moduleRadiusBackground, moduleRadiusBackground);
    }
  }

  for (int gridY=0; gridY<tileCount; gridY++) {
    for (int gridX=0; gridX<tileCount; gridX++) {

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

      fill(moduleColorForeground, moduleAlphaForeground);
      ellipse(posX, posY, moduleRadiusForeground, moduleRadiusForeground);
    }
  }



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

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

  if (keyCode == UP) moduleRadiusBackground += 2;
  if (keyCode == DOWN) moduleRadiusBackground = max(moduleRadiusBackground-2, 10);
  if (keyCode == LEFT) moduleRadiusForeground = max(moduleRadiusForeground-2, 5);
  if (keyCode == RIGHT) moduleRadiusForeground += 2;
}

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

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

リファレンス