////////////////////////////////////////////////////////////////////////////////
// Note from the Scriptographer.org Team
//
// In Scriptographer 2.9, we switched to a top-down coordinate system and
// degrees for angle units as an easier alternative to radians.
// 
// For backward compatibility we offer the possibility to still use the old
// bottom-up coordinate system and radians for angle units, by setting the two
// values bellow. Read more about this transition on our website:
// http://scriptographer.org/news/version-2.9.064-arrived/

script.coordinateSystem = 'bottom-up';
script.angleUnits = 'radians';

/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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/
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 

var values = {
	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() {
	values = Dialog.prompt('supershape 2d', {
		rnd: {description: 'random shape on click on/off (1/2)'},
		drw: {description: 'use as shape or as draw tool on/off (1/2)'},
		cls: {description: 'close the path on/off (1/2)'},
		amount: {description: 'smoothness of the shape path'},
		size: {description: 'size of supershape object'},
		count: {description: 'loop count of the shape processing'},
		mr: {description: 'm - first of four shape parameter (or the random range)'},
		n1r: {description: 'n1 - second of four shape parameter (or the random range)'},
		n2r: {description: 'n2 - third of four shape parameter (or the random range)'},
		n3r: {description: 'n3 - fourth of four shape parameter (or the random range)'}
	}, values);
}


function onMouseDown(event) {
	
	if(values.rnd == 1) {
		m = Math.random() * values.mr;
		n1 = Math.random() * values.n1r;
		n2 = Math.random() * values.n2r;
		n3 = Math.random() * values.n3r;
	} else {
		m = values.mr;
		n1 = values.n1r;
		n2 = values.n2r;
		n3 = values.n3r;
	}
	
	if(values.drw !== 1) {
		supershape(event);
	}
    
}

function onMouseDrag(event) {
	if(values.drw == 1) {
		supershape(event);
	}
}

function supershape(event) {
	paths = [];
    for (i = 0; i < values.amount; i++) {
        paths.push(new Path());
    }
    var basepath = new Path();	
	
	for (i = 0; i < values.amount; i++) {
		
    	phi = i * (values.count * Math.PI) / values.amount;
		
		var r = 0;
	    var t1 = 0;
	    var t2 = 0;
	    var a = 1;
	    var b = 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);
		var vector;
	    if (Math.abs(r) == 0) {
			vector = new Point(0, 0);
	    } else {
		  r = 1 / r;
		  vector = new Point(Math.cos(phi), Math.sin(phi)) * r;
	    }
	
		
        paths[i] = event.point + vector * values.size;
		
		if(i == 0) {
			basepath.moveTo(paths[i]);
		} else {
			basepath.lineTo(paths[i]);
		}
    }
	if(values.cls == 1) {
		basepath.lineTo(paths[0]);
	}
}

