// draws a fishbone like thing (for lack of a better name) by duplicateing and rotating the line as you draw it. 
// This was a bit of an accident I created while trying to create something else.
// It creates some very interesting patterns. Moving the mouse slowly spaces 
// the ribs by the minimum distance. Moving the mouse fast leaves more space. Have fun!
// Keegan Green

function onInit() {
	mindist = 15;
	circ = 1;
	diameter = 5;
	spine = 1;
	angle = 90;
	theta = angle/(180/Math.PI);
	riblen = 0;
	randang = 0;
	fan = 0; // use this to create a line from origin
}

function onOptions() {
	
	var values = Dialog.prompt("Fishbones", [
	{ value: mindist, description: "Minimum Distance between vertabrae"},
	{ value: circ, description: "Circles on(1) or off(0)"},
	{ value: diameter, description: "Diameter of circles"},
	{ value: spine, description: "Spine on(1) or off(0)"},
	{ value: angle, description: "Angle of ribs relative to spine"},
	{ value: randang, description: "Use random angle (1) or off(0)"},
	{ value: riblen, description: "Rib Length: entire path (0), last segment (1)"},
	]);

	if (values != null) {
		mindist = values[0];
		circ = values[1];
		diameter = values[2];
		spine = values[3];
		angle = values[4];
		randang = values[5];
		riblen = values[6];
	}
	theta = angle/(180/Math.PI);//convert degrees to a number .rotate can use.
	
}


function onMouseDown(event) {
	//initialize all nessicary lines
    mainpath = new Path();
	mainpath.moveTo(event.point);	
	prevpoint = event.point; //stores previous point to calculate distance
	gp = new Group();
	gp.appendChild(mainpath);
	

}

function onMouseUp(event) {
	mainpath.pointsToCurves();
	
	if (spine == 0){ //remove the main line if user doesn't want it
		mainpath.remove();
	}

}

function onMouseDrag(event) {
    var point = event.point;
	// draws the main line were the mouse moves
	mainpath.lineTo(event.point);
	
	
	dist = event.point.getDistance(prevpoint);
	// draw the circle, and ribs
	if (dist > mindist){		
		if (randang == 1){
			theta = Math.random()* Math.PI;
		}
		if (riblen == 0){
			// draw ribs by duplicating and rotating all of mainpath by theta and -theta
			rib1 = mainpath.clone();
			rib1.rotate(theta, event.point);
			rib2 = mainpath.clone();
			rib2.rotate(-theta, event.point);
		} else if (riblen == 1) {
			// draw ribs by duplicating and rotating the previous segment by theta and -theta
			prevseg = new Path(); // this might be a silly way to do this, but here a
			prevseg.moveTo(prevpoint);// new line is created the length of the previous segment
			prevseg.lineTo(event.point); 
			rib1 = prevseg.clone(); //that line is then cloned and rotaded twice, then removed.
			rib1.rotate(theta, event.point);
			rib2 = prevseg.clone();
			rib2.rotate(-theta, event.point);
			prevseg.remove();
		
		}
		// simplify the number of points in the ribs
		rib1.pointsToCurves();
		rib2.pointsToCurves();
		//add ribs to the group
		gp.appendChild(rib1);
		gp.appendChild(rib2);
		// draw circle if the user wants it
		if (circ == 1){
			rect = new Rectangle(0, 0, diameter, diameter);
    		rect.center = event.point;		
    		var c = activeDocument.createOval(rect);
			with(c.style){fill.color = new GrayColor(0);}//trying to fill circle with white, not working
			c.moveAbove(mainpath);
			gp.appendChild(c);
		}
		prevpoint = event.point;
	}
	
}


