// +-----------------------
// | (c)2006 dvb@omino.com, subservient astronaut blog
// | 
// | file: apse_02_drawing.jsx
// | astro_photoshop_scripting_examples: drawing
// |
// | How to draw a picture

// +-------------------------------------------
// | Build up a list of selection points 
// | to form a circle
function circlePath(x,y,r)
{
	var result = new Array();
	var i;
	var pi = 3.14159265358979323846264338327950288;
	var points = 120;
	for(i = 0; i <= points; i++)
	{
		var theta = 2 * pi * i / points;
		var point = new Array(
				Math.cos(theta) * r + x,
				Math.sin(theta) * r + y);
		result[result.length] = point; // add to end of array
	}
	return result;
}

// +-------------------------------------------
// | Select a circle with a feathered edge,
// | on the _current document_
function selectCircle(x,y,r,feather)
{
	var path = circlePath(x,y,r);
	
	// | select() can *only* be invoked on
	// | the currently active document.
	// | (Else, Photoshop gives you an error.)
	app.activeDocument.selection.select(path);
	app.activeDocument.selection.feather(feather);
}

function main()
{
	// | Slam the global state so we can talk in pixels.
	// | A nicer script would save and restore it.
	app.preferences.rulerUnits = Units.PIXELS;
	
	// | Create a new document via the app.documents array.
	// | It's a lot of constants and options!
	var newDoc = app.documents.add(400,400,72,
		"apse_02",
		NewDocumentMode.RGB,
		DocumentFill.TRANSPARENT);

	
	app.activeDocument = newDoc;

	// | This circle exceeds the document bounds;
	// | photoshop will clip it.
	selectCircle(300,300,120,0);
	app.activeDocument.selection.fill(app.foregroundColor);

	// | Now paint a bunch of other circles...
	for(k = 0; k < 20; k++)
	{
		selectCircle(Math.random() * 400, Math.random() * 400, // center
			Math.random() * 90 + 30, // radius
			Math.random() * 60 + 3); // feather
		var aColor = new SolidColor();
		aColor.rgb.red = 128 + Math.random() * 127;
		aColor.rgb.green = 0;
		aColor.rgb.blue = Math.random() * 127;
		
		// fill first with the foreground color, then red
		// (it is feathered so this makes a dark edge)
		app.activeDocument.selection.fill(app.foregroundColor);
		app.activeDocument.selection.fill(aColor);
	}

	app.activeDocument.selection.deselect();
	
	return "ok"; // ExtendScript debugger prints out final value. Ok!
}

main(); // I like to box everything up in "main".

// | The End
