Skip to content

Instantly share code, notes, and snippets.

@pandr
Created November 9, 2017 06:44
Show Gist options
  • Save pandr/18a38082d1dc6b84e625bb5b8e7e6477 to your computer and use it in GitHub Desktop.
Save pandr/18a38082d1dc6b84e625bb5b8e7e6477 to your computer and use it in GitHub Desktop.
function setup()
{
createCanvas(700, 400);
background(255);
}
var deg60 = Math.PI/3;
function addVector(x,y,angle,length)
{
return [x + length*Math.cos(angle), y + length*Math.sin(angle)];
}
function drawSide(x,y,angle,length,level)
{
if (level == 0)
{
var [x2, y2] = addVector(x,y,angle,length);
line(x,y,x2,y2);
}
else
{
length /= 3;
level -= 1;
drawSide(x,y,angle,length,level);
[x,y] = addVector(x,y,angle,length);
angle -= deg60;
drawSide(x,y,angle,length,level);
[x,y] = addVector(x,y,angle,length);
angle += 2*deg60;
drawSide(x,y,angle,length,level);
[x,y] = addVector(x,y,angle,length);
angle -= deg60;
drawSide(x,y,angle,length,level);
}
}
var t = 0;
function draw()
{
background(255);
var mx = 350;
var my = 200;
var length = 300;
var level = 5 - Math.abs(Math.floor(t*10) % 10 - 5);
var [x,y] = addVector(mx,my,t - 2.5*deg60,length / Math.sqrt(3));
drawSide(x,y,t,length,level);
[x,y] = addVector(x,y,t,length);
var angle = t + 2*deg60;
drawSide(x,y,angle,length,level);
[x,y] = addVector(x,y,angle,length);
angle += 2*deg60;
drawSide(x,y,angle,length,level);
t += 0.003;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment