/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
supershape 2d basic v0.1a
uses the supershape algo. from Johan Gielis

	"A Belgian Biologist named Johan Gielis discovered a 
	formula that creates a vast diversity of natural shapes. 
	By tweaking four parameters it can produce everything 
	from simple triangles and pentagons, to stars, spirals 
	and petals."*
	
	* http://raa.ruby-lang.org/project/supershape/


adapted from 
http://astronomy.swin.edu.au/~pbourke/curves/supershape/
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 

function onInit() {
	
	amount = 150;
	size = 80;
	count = 2;
	mr = Math.random() * 15;
	n1r = Math.random() * 3;
	n2r = Math.random() * 3;
	n3r = Math.random() * 3;
	cls = 1;
	rnd = 1;
	drw = 1;
	
	
}

function onOptions() {
	
	var values = Dialog.prompt("supershape 2d", [
	{ value: rnd, description: "random shape on click on/off (1/2)"},
	{ value: drw, description: "use as shape or as draw tool on/off (1/2)"},
	{ value: cls, description: "close the path on/off (1/2)"},
	{ value: amount, description: "smoothness of the shape path"},
	{ value: size, description: "size of supershape object"},
	{ value: count, description: "loop count of the shape processing"},
	{ value: mr, description: "m - first of four shape parameter (or the random range)"},
	{ value: n1r, description: "n1 - second of four shape parameter (or the random range)"},
	{ value: n2r, description: "n2 - third of four shape parameter (or the random range)"},
	{ value: n3r, description: "n3 - fourth of four shape parameter (or the random range)"}
	]);

	if (values != null) {
		rnd = values[0];
		drw = values[1];
		cls = values[2];
		amount = values[3];
		size = values[4];
		count = values[5];
		mr = values[6];
		n1r = values[7];
		n2r = values[8];
		n3r = values[9];
	}
	
}

function onMouseDown(event) {
	
	if(rnd == 1) {
		m = Math.random() * mr;
		n1 = Math.random() * n1r;
		n2 = Math.random() * n2r;
		n3 = Math.random() * n3r;
	} else {
		m = mr;
		n1 = n1r;
		n2 = n2r;
		n3 = n3r;
	}
	
	if(drw !== 1) {
		supershape(event);
	}
    
}

function onMouseDrag(event) {
	if(drw == 1) {
		supershape(event);
	}
}

function supershape(event) {
	paths = new Array(amount);
    for (i = 0; i < amount; i++) {
        paths[i] = new Path();
    }
    basepath = new Path();	
	
	for (i = 0; i < amount; i++) {
		
    	phi = i * (count * Math.PI) / amount;
		
		var r = new Number(0.0);
	    var t1 = new Number(0.0);
	    var t2 = new Number(0.0);
	    var a = new Number(1);
	    var b = new Number(1);
	   
	    t1 = Math.cos(m * phi / 4) / a;
	    t1 = Math.abs(t1);
	    t1 = Math.pow(t1,n2);
	
	    t2 = Math.sin(m * phi / 4) / b;
	    t2 = Math.abs(t2);
	    t2 = Math.pow(t2,n3);
	
	    r = Math.pow(t1+t2,1/n1);
	   
	    if (Math.abs(r) == 0) {
		  x = 0;
		  y = 0;
	    } else {
		  r = 1 / r;
		  x = r * Math.cos(phi);
		  y = r * Math.sin(phi);
	    }
		ax = x*size;
		ay = y*size;
		
        paths[i] = event.point.add(ax,ay);
		
		if(i == 0) {
			basepath.moveTo(paths[i]);
		} else {
			basepath.lineTo(paths[i]);
		}
    }
	if(cls == 1) {
		basepath.lineTo(paths[0]);
	}
}

