0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Sierpinski gasketをランダムドットで描画する

Posted at

この方法でシェルピンスキーのギャスケットを描くプログラム。

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));
}

image.png
image.png
image.png
image.png
数値変更
image.png
num=6,div3,*2
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?