Code:
var width = 600;
var height = 600;
var size = 500;
var dx = -300, dy = -300;
function sierpinski(Ax,Ay,Bx,By,Cx,Cy,d) {
if(d>0) {
var pointAx = (Bx + Cx) / 2;
var pointAy = (By + Cy) / 2;
var pointBx = (Ax + Cx) / 2;
var pointBy = (Ay + Cy) / 2;
var pointCx = (Ax + Bx) / 2;
var pointCy = (Ay + By) / 2;
var d2 = d-1;
sierpinski(Ax,Ay,pointBx,pointBy,pointCx,pointCy,d2);
sierpinski(pointCx,pointCy,pointAx,pointAy,Bx,By,d2);
sierpinski(pointBx,pointBy,pointAx,pointAy,Cx,Cy,d2);
}
else {
track.addLine(new Line(Ax + dx, Ay + dy, Bx + dx, By + dy));
track.addLine(new Line(Bx + dx, By + dy, Cx + dx, Cy + dy));
track.addLine(new Line(Cx + dx, Cy + dy, Ax + dx, Ay + dy));
}
}
var midPointX = width/2;
var midPointY = height/2;
var deep = 6;
var ri = (size/6) * Math.sqrt(3);
var ru = (size/3) * Math.sqrt(3);
var pointAx = midPointX-(size/2);
var pointAy = midPointY+ri;
var pointBx = midPointX+(size/2);
var pointBy = midPointY+ri;
var pointCx = midPointX;
var pointCy = midPointY-ru;
sierpinski(pointAx,pointAy,pointBx,pointBy,pointCx,pointCy,deep);
When you see var deep = 6, that represents how many layers will be rendered.