
#include "reflection_tessellation.jsx"

#include "../omino_adobe_script_suite/src/shared/ominoDialogMaker.jsx"

var gLastReflectionTriangles1;

{
    var omd = newOminoDialog("Circular Gauge");

	omd.boxedText(5,"This Illustrator script tesselates rectangles with a reflected image.");
    omd.separator();
	omd.openFile("Image","f","");

	omd.number("Scale","scale",1.0);

	omd.number("Cell Size X","cellSizeX",100);
    omd.number("Y","cellSizeY",100);
	omd.number("Alternating Inset","altInset",50);
    omd.number("Cells Across","cellsAcross",7);
    omd.number("Down","cellsDown",7);
	omd.checkbox("Edge Continuers","edgies",true,"");

	omd.set(gLastReflectionTriangles1);
    var r = omd.run();
	
	if(r != null)
	{
		gLastReflectionTriangles1 = r;

		// work on a new layer of the current doc.
		var doc = documents[0];
		var layer = doc.layers.add();
		layer.name = "tessellated_triangles";
		
		var docCx = doc.width / 2;
		var docCy = doc.height / 2;
		
		var lowX = docCx - r.cellSizeX * r.cellsAcross / 2;
		var lowY = docCy - r.cellSizeY * r.cellsDown / 2;

		var t = tNew();
		
		// lay down the point mesh.
		for(var j = 0; j <= r.cellsDown; j++)
		{
			var y = lowY + j * r.cellSizeY;
			var left = (j % 2) ? lowX + r.altInset : lowX;
			for(var i = 0; i <= r.cellsAcross; i++)
			{
				var x = left + i * r.cellSizeX;
				tAddPoint(t,x,y);
			}
		}
		
		// lay down the triangles
		for(var j = 0; j < r.cellsDown; j++)
		{
			var b = r.cellsAcross + 1;
			var ix = b * j;
			for(var i = 0; i < r.cellsAcross; i++)
			{
				if((j % 2) == 0) // 0, 2, 4
				{
				tAddPoly(t,[t.points[ix + i],t.points[ix + i + b],t.points[ix + i + 1]]);
				tAddPoly(t,[t.points[ix + i + 1],t.points[ix + i +b +1],t.points[ix + i + b]]);
				}
				else // 1,3,5
				{
				tAddPoly(t,[t.points[ix + i],t.points[ix + i + b +1],t.points[ix + i + b]]);
				tAddPoly(t,[t.points[ix + i],t.points[ix + i +1],t.points[ix + i + b + 1]]);
				}
			}
		}
	
		var po = tFindCenterPoly(t)

		// seed the center and draw
		var g = layer.groupItems.add();
		g.name = "images";
		var pa = tNewPatch(po.centerX,po.centerY,0,0);
		pa.scale = r.scale;
		po.pa = pa; // stash the patch in the poly
		tDrawKTessellation(g,t,r.f,po);
		
		// draw the edges
		var g = layer.groupItems.add();
		g.name = "edges";
		tDrawAllEdges(g,t);

		// draw the center one
		tDrawPoly(layer,po,1,0,0,.3).name = "center_poly";

	}
}