【p5.js】シェルピンスキーのギャスケット

function setup() {
  createCanvas(400, 400);
  noStroke();
}

function draw() {
  background(220);
  drawTriangle(width/2, 50, 300, 5);
}

function drawTriangle(x, y, length, level) {
  fill(0);
  triangle(x, y, x+length*sin(PI/6), y+length*cos(PI/6), x-length*sin(PI/6), y+length*cos(PI/6));
  if (level > 0) {
    fill(255);
    triangle(x, y, x+length*sin(PI/6), y+length*cos(PI/6), x-length*sin(PI/6), y+length*cos(PI/6));
    length /= 2;
    drawTriangle(x, y, length, level-1);
    drawTriangle(x+length*sin(PI/6), y+length*cos(PI/6), length, level-1);
    drawTriangle(x-length*sin(PI/6), y+length*cos(PI/6), length, level-1);
  }
}

今回は、以下のように出力される。

p5.js Web Editor
A web editor for p5.js, a JavaScript library with the goal of making coding accessible to artists, designers, educators,...

参考

シェルピンスキーのギャスケット - Wikipedia
シェルピンスキーのギャスケット
(著)山たー
タイトルとURLをコピーしました