Skip to content

Instantly share code, notes, and snippets.

@ogrew
Last active April 13, 2022 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ogrew/d0b9f76a3e89f6b96df89aff55c2dc80 to your computer and use it in GitHub Desktop.
Save ogrew/d0b9f76a3e89f6b96df89aff55c2dc80 to your computer and use it in GitHub Desktop.
シェルピンスキーのギャスケット
let W = 1200;
let H = 1200;
function setup() {
createCanvas(W, H);
background(255);
smooth();
noLoop();
noStroke();
pixelDensity(12);
}
function draw() {
fill(0);
sierpinski_tri(W/2, 50, H, 12);
}
function sierpinski_tri(x, y, len, depth) {
var x1 = x + len*sin(PI/6);
var x2 = x - len*sin(PI/6);
var yy = y + len*cos(PI/6);
fill(0);
triangle(x, y, x1, yy, x2, yy);
depth -= 1;
if(depth > 0) {
fill(255);
triangle(x, y, x1, yy, x2, yy);
var ll = len / 2;
sierpinski_tri(x, y, ll, depth);
sierpinski_tri(x+ll*sin(PI/6), y+ll*cos(PI/6), ll, depth);
sierpinski_tri(x-ll*sin(PI/6), y+ll*cos(PI/6), ll, depth);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment