日常の進捗

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

Mod: Generative Design / P_2_1_3_02

f:id:takawo:20180112231220g:plain

当初実行画面を見た時に,アルゴリズムが読めなくて困惑した.

コードの込み入った部分だけを取り出して読んでみると,for文を使って矩形領域の4辺をなぞるように変数x2,y2を使って座標を移動している.周囲をなぞるようなアルゴリズムはちょうど今日見た以下のようなプログラムでも使えそうと思った.

コード(アルゴリズム確認用)

colorMode(HSB, 360, 100, 100, 100);
background(0, 0, 100);
stroke(random(360), 80, 100);
int count = 10;
float x1 = width/2;
float y1 = height/2;
float x2 = 0;
float y2 = 0;
for (int side = 0; side < 4; side++) {
  for (int i = 0; i < count; i++) {
    switch(side) {
    case 0:
      x2 += width/count;
      y2 = 0;
      break;
    case 1:
      x2 = width;
      y2 += height/count;
      break;
    case 2:
      x2 -= width/count;
      y2 = height;
      break;
    case 3:
      x2 = 0;
      y2 -= height/count;
      break;
    }
    line(x1, y1, x2, y2);
  }
}

コード

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

boolean savePDF;

float tileCountX = 5;
float tileCountY = 5;
int count = 10;
int colorStep = 20;
int lineWeight = 1;
int strokeColor = 0;
color backgroundColor;
int drawMode = 1;

void setup() {
  size(800, 800);
  colorMode(HSB, 360, 100, 100, 100);
  backgroundColor = color(random(360), 100, 50);
}

void draw() {
  if (savePDF) {
    beginRecord(PDF, timestamp() + ".pdf");
    colorMode(HSB, 360, 100, 100, 100);
  }
  strokeWeight(0.5);
  strokeCap(ROUND);
  tileCountX = constrain(mouseX/30, 1, mouseX/30);
  tileCountY = constrain(mouseY/30, 1, mouseY/30);

  background(backgroundColor);

  for (int gridY=0; gridY<= tileCountY; gridY++) {
    for (int gridX=0; gridX<= tileCountX; gridX++) {
      float tileWidth = width / tileCountX;
      float tileHeight = height / tileCountY;
      float posX = tileWidth * gridX;
      float posY = tileHeight * gridY;

      float x1 = tileWidth / 2;
      float y1 = tileHeight / 2;
      float x2 = 0;
      float y2 = 0;

      pushMatrix();
      translate(posX, posY);
      stroke(0, 0, 100);
      strokeWeight(5);
      point(0, 0);
      for (int side = 0; side < 4; side++) {
        for (int i = 0; i < count; i++) {
          switch(side) {
          case 0:
            x2 += tileWidth/count;
            y2 = 0;
            break;
          case 1:
            x2 = tileWidth;
            y2 += tileHeight/count;
            break;
          case 2:
            x2 -= tileWidth/count;
            y2 = tileHeight;
            break;
          case 3:
            x2 = 0;
            y2 -= tileHeight/count;
            break;
          }
          strokeWeight(1);
          line(x1, y1, x2, y2);
        }
      }
      popMatrix();
    }
  }

  if (savePDF) {
    endRecord();
  }
}


void keyPressed() {
  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);
}

リファレンス