A Day In The Life

とあるプログラマの備忘録

Processingでお絵描きをしてみる

最近Processingというお絵描き用プログラミング言語で遊んでいます。
手軽にいろいろできて面白いです。構文はJavaの簡易版といった感じなので、Javaがわかる人だとすぐにプログラムできるようになると思います。

サンプル

sample1

static final int LENGTH = 400;
void setup() {
  size(LENGTH, LENGTH);
  background(255);
  noStroke();
  for (int i = 0; i < 30; i++) {
    color cl = color(255, random(255), 255, 128);
    fill(cl);
    float r = random(200);
    ellipse(random(LENGTH), random(LENGTH), r, r);
  }
}

binary tree

class Point {
  final float x;
  final float y;
  public Point(float x, float y) {
    this.x = x;
    this.y = y;
  }
}

static final int ANGLE = 30;
static final int WINDOW_WIDTH = 600;
static final int WINDOW_HEIGHT = 400;

void setup() {
  size(WINDOW_WIDTH, WINDOW_HEIGHT);
  background(255);
  stroke(126);
  Point p = new Point(WINDOW_WIDTH / 2, WINDOW_HEIGHT);
  drawTree(0, 150, p);
}

void drawTree(int currentAngle, float lineLength, Point origin) {
  if (lineLength < 3) return;
  float newLineLength = lineLength * 0.7;
  int sign = 1;
  for (int i = 0; i < 2; i++) {
    int angle = currentAngle + (ANGLE * sign);
    Point newPoint = calcPoint(angle, newLineLength, origin);
    line(origin.x, origin.y, newPoint.x, newPoint.y);
    drawTree(angle, newLineLength, newPoint);
    sign *= -1;
  }
}

Point calcPoint(int angle, float lineLength, Point p) {
  float x = (float)(p.x + lineLength * sin(radians(angle)));
  float y = (float)(p.y - lineLength * cos(radians(angle)));
  return new Point(x, y);
}

sample2

class Point {
  final float x;
  final float y;
  public Point(float x, float y) {
    this.x = x;
    this.y = y;
  }
}

void setup() {
  size(400, 400);
  background(255);
  drawLines(0, new Point(200, 200));
}

void drawLines(int count, Point p) {
  if (count > 3) return;
  count++;
  for (int i = 0; i < 360; i += 30) {
    Point p2 = calcPoint(i, 50, p);
    line(p.x, p.y, p2.x, p2.y);
    drawLines(count, p2);
  }
}

Point calcPoint(int angle, float lineLength, Point p) {
  float x = (float)(p.x + lineLength * sin(radians(angle)));
  float y = (float)(p.y - lineLength * cos(radians(angle)));
  return new Point(x, y);
}

sample3

class Point {
  final float x;
  final float y;
  public Point(float x, float y) {
    this.x = x;
    this.y = y;
  }
}

void setup() {
  size(400, 400);
  background(255);
  drawCircle(100, new Point(200, 200));
  saveFrame("sample2.png");
}

void drawCircle(float lineLength, Point p) {
  if (lineLength < 49) return;
  float newLength = lineLength * 0.7;
  for (int i = 0; i < 360; i += 45) {
    Point p2 = calcPoint(i, 50, p);
    color cl = color(random(255), 255, 255, 200);
    fill(cl);
    noStroke();
    float r = random(50);
    ellipse(p2.x, p2.y, r, r);
    drawCircle(newLength, p2);
  }
}

Point calcPoint(int angle, float lineLength, Point p) {
  float x = (float)(p.x + lineLength * sin(radians(angle)));
  float y = (float)(p.y - lineLength * cos(radians(angle)));
  return new Point(x, y);
}

ちなみにこのProcessingはJavaVM上で動作します。JavaVM上で動くのでJavaのライブラリも使うことができます。

参考