日常の進捗

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

極座標で10print

Twitterのタイムラインに#10print で面白い画像が流れてきたので、遡って見ながら頭のなかでどう書くのか考えたりしていた。

twitter.com

その中の一つで、極座標(原点からの距離rと角度theta)をつかって円周上の点をプロットしながら描いていくものがあって面白そうだったので書いてみた。

円周上の点を打ちながら半径を広げていく過程でランダムで回転の方向をずらしている。

コード

float n = 20;
float rStep = 20;

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

void draw() {
  background(220, 20, 20);
  translate(width/2, height/2);
  stroke(0, 0, 100);
  for (float r = 50; r < sqrt(sq(width/2)+sq(height/2)); r += rStep) {
    for (float angle = 0; angle < 360; angle += 360/n) {
      float direction = (random(1) < 0.5) ? -1:1; 
      float thetaA = radians(angle + direction * 360/n/2); 
      float thetaB = radians(angle - direction * 360/n/2); 
      float x1 = r * cos(thetaA); 
      float y1 = r * sin(thetaA); 
      float x2 = (r+rStep) * cos(thetaB); 
      float y2 = (r+rStep) * sin(thetaB); 
      line(x1, y1, x2, y2);
    }
  }
}