この方法でシェルピンスキーのギャスケットを描くプログラム。
SierpinskiGasketWithRandom.pde
float cx = 300; // 中心のx
float cy = 300; // 中心のy
float r = 300; // 外接円半径(頂点までの距離)
PVector v; // 移動する点
int num=3; // 最初のn多角形
PVector[] vs = new PVector[num];// n多角形の頂点
void setup() {
size(600, 600);
background(255);
frameRate(999);
// n多角形の頂点を求める
for (int i = 0; i < num; i++) {
float th = radians(i*360/float(num)+270);
vs[i] = new PVector(cx + r * cos(th), cy + r * sin(th));
}
// 開始点(任意の位置)
v = new PVector(cx, cy);
}
void draw() {
// ランダムに頂点を選ぶ
int idx = int(random(num));
PVector target = vs[idx];
// v と target の中点(3の場合)をとる
v.add(target);
v.div(num-1);
// numが大きくなると、全体が小さくなるので、適当に大きくする
set(int(v.x*(num-2)), int(v.y*(num-2)), color(0));
}





