function setup() {
	g_side=50*vmin();
	var canvas=document.getElementById('c');
	canvas.width=g_side; canvas.height=g_side;
	g_ctx=canvas.getContext('2d');
	g_step=g_side/800;
	home(); cs();
}
function vmin() {
	var w=innerWidth; var h=innerHeight;
	var v=w;
	if (h<w) v=h;
	return v/100;
}
function resize() {
	var side=50*vmin();
	if (side!=g_side) setup();
}
function fd(d) {
	g_ctx.beginPath();
	g_ctx.moveTo(g_x,g_y);
	var rad=g_h*Math.PI/180.0;
	var dx=g_step*d*Math.sin(rad);
	var dy=-g_step*d*Math.cos(rad);
	g_x=g_x+dx; g_y=g_y+dy;
	if (g_pen=='down') {
		g_ctx.lineTo(g_x,g_y);
		g_ctx.stroke();
		store();
	} else {
		g_ctx.moveTo(g_x,g_y);
	}
}
function store() {
	g_xx.push(Math.round(g_x)); 
	g_yy.push(Math.round(g_y));
}
function rt(angle) {
	g_h=g_h+angle;
	g_h=g_h%360;
}
function home() {
	g_x=g_side/2; g_y=g_side/2; g_h=0;
}
function cs() {
	g_ctx.clearRect(0,0,g_side,g_side);
	g_pen='down'; g_xx=[]; g_yy=[];
}
function spiral(angle,d=1) {
	fd(d); rt(angle); d++;
	if (d<500) spiral(angle,d);
}
function spirals(angle=75) {
	cs(); home();
	spiral(angle); angle++;
	if (angle<145 && g_running) g_timer=setTimeout(spirals,g_delay,angle);
}
function polygon(n,d=100) {
	var angle=360/n;
	for (var i=0; i<n; i++) { fd(d); rt(angle);}
}
function polygons(n) {
	var k=n*2;
	var d=1000/n;
	for (var i=0; i<k; i++) {
		polygon(n,d); rt(360/k)
	}
}
function polys(n=3) {
	cs(); home();
	polygons(n); n++;
	if (n<21 && g_running) g_timer=setTimeout(polys,g_delay,n);
}
function star(d=350) {
	for (var i=0; i<5; i++) {
		fd(d); rt(144);
	}
}
function stars(n=10) {
	for (var i=0;i<n;i++) {
		star(); rt(360/n);
	}
}
function starry(n=3) {
	cs(); home();
	stars(n); n++;
	if (n<41 && g_running) {
		g_timer=setTimeout(starry,g_delay,n);
	}
}
function circle(diameter=100) {
	var circumference=Math.PI*diameter;
	var d=circumference/360;
	polygon(360,d);
}
function pu() {
	g_pen='up';
}
function pd() {
	g_pen='down';
}
function bg(colr='white') {
	g_ctx.fillStyle=colr;
	g_ctx.fillRect(0,0,g_side,g_side);
}
function pc(colr='black') {
	g_ctx.strokeStyle=colr;
}
function pw(w=1) {
  g_ctx.lineWidth=w;
}
function circles(n,dd) {
  for (var i=0;i<10;i++) {
    var diameter=360;
    for (var j=0;j<n;j++) {
      circle(diameter);
      diameter=diameter-dd;
    }
    rt(36);
  }
}
var g_spin=true;
function wow() {
	g_spin=!g_spin;
	if (g_spin) {
		spin();
	} else {
	  pw(4);
	  bg('black');
	  pc('white'); circles(10,9);
	  pc('yellow'); circles(4,23);
	  pc('blue'); circles(4,12);
	  pc('orange'); circles(4,45);
	  pc('pink'); circles(4,47);
	  pw();
	 }
}
function lt(angle) {
	rt(-angle);
}
function bk(d) {
	fd(-d);
}
// Returns a random integer between min (included) and max (included)
function Rand(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
function vee(d,w) {
	d=Rand(d-3,d+3);
  if (d>10) {
    var f=.8;
    var dlim=20;
    pw(w); pc('brown');
    if (d<dlim) pc('lime');
    lt(30);
    fd(d); vee(d*f,w*f);
    pw(w); pc('brown');
    if (d<dlim) pc('lime');
    bk(d); rt(60);
    fd(d); vee(d*f,w*f);
    pw(w); pc('brown');
    if (d<dlim) pc('lime');
    bk(d); lt(30);
  }
}
function tree() {
  pc('brown');
  pu(); bk(350); pd();
  pw(4); fd(150); vee(100,4);
}
function join(x1,y1,x2,y2) {
	g_ctx.beginPath();
	g_ctx.moveTo(x1,y1);
	g_ctx.lineTo(x2,y2);
	g_ctx.stroke();
}
function cpolygon(n) {
	home();
	var r=330; var a=Math.PI/n;
	var d=2*r*Math.sin(a);
	var dd=r*Math.cos(a);
	pu(); bk(dd); lt(90); bk(d/2);
	pd();
	polygon(n,d);
}
function rose(n=11) {
	cs();
	cpolygon(n);
	for (var i=0;i<n-1;i++) {
		for (var j=i+1;j<n;j++) {
			join(g_xx[i],g_yy[i],g_xx[j],g_yy[j]);
		}
	} 
}
function roses(n=5) {
	rose(n);
	n++;
	if (n<33 && g_running) g_timer=setTimeout(roses,g_delay,n);
}
function stop () {
	clearTimeout(g_timer); g_running=false;
}
function fast() {
	g_delay=200;
}
function slow() {
	g_delay=1000;
}
function reset() {
  stop(); cs(); home(); bg(); pc(); pw();
}
function spike() {
	fd(300); rt(75); fd(100); rt(165);
}
function spikes() {
	for (var i=0;i<3;i++) spike();
}
function spikes10() {
	for (var i=0;i<10;i++) {spikes(); rt(36);}
}
function spikey() {
	cs();
	g_ctx.lineCap='round';
	pw(12); pc(); spikes10();
	pw(4); pc('white'); spikes10();
}
function spin() {
	spikey(); lt(1);
	if (g_running) {
		g_timer=setTimeout(spin,5);
	}
}
var g_colours=['red','orange','yellow','green','blue','indigo','violet'];
var g_a;
function cvee(d,cn) {
	cn++;
	if (cn==g_colours.length) cn=0;
  if (d>3) {
    var f=.8;
  	lt(g_a);
		pc(g_colours[cn]); pd();
    fd(d); cvee(d*f,cn); pu();
    bk(d); rt(g_a*2);
    pc(g_colours[cn]); pd();
    fd(d); cvee(d*f,cn); pu();
    bk(d); lt(g_a);
	}
}
function ctree() {
	cs(); home();
	pu(); bk(100); pd();
	bg('black');
  cvee(80,0);
}
function ctrees(a=0) {
	g_a=a;
	ctree();
	if (a<45 && g_running) {
		a++;
		if (g_running) {
			g_timer=setTimeout(ctrees,0,a);
		}
	}
}

