Processingでランダムウォークを作ってみた。

Processingを使ったらいろんなもの作れて楽しそう!と思ったけど、何を作ればいいかが思いつかなかったので、HALさんの記事の一番上にあったやつを作ってみました。

HALさんの記事↓

www.fal-works.jp

作ったもの

youtu.be

コード


void setup() {
  size(1000, 800);
  background(200);
}

float x = 500;
float y = 500;
float newX;
float newY;
int d = 15;
int i;
int buffer;
int deg = 45;
int division = 360/deg;
int step = 30;

void draw() {
  noStroke();
  fill(255, 5);
  rect(0, 0, 1000, 800);

  for (int j =0; j<step; j++) {
    do {
      i = (int)random(0, division);
      newX = x+d*cos(radians(deg*i));
      newY = y+d*sin(radians(deg*i));
    } while (newX<0||newX>width||newY<0||newY>height||(buffer+division/2)%division==i);
    buffer = i;

    noFill();
    stroke(0, 0, 0);
    line(x, y, newX, newY);

    x = newX;
    y = newY;
  }
}