package CookieCutter.g1;

import CookieCutter.*;

/**
 * @author mayz
 */
public class DefaultCookiePlacer implements CookiePlacer {

	public void placeCookies(Cookie[] iCookies) {
		for (int i = 0; i < iCookies.length; i++) {
			Cookie c = iCookies[i];
			
			Vertex center = c.getCentroid();			
			center.setYpos(0.5);
			center.setXpos(0.5 + i);
			
			Cookie bestCookie = null;
			Box bounds = null;
			
			double width;
			double bestWidth = Double.MAX_VALUE;

			for (int k = 0; k < 360; k++) {
				c = c.transform(center, Math.PI/180.0);
				width = c.getBounds().getWidth();
				if (width < bestWidth) {
					bestWidth = width;
					bestCookie = c;
//					System.out.println(i + ": new best width " + Point.nf.format(width) +
//						" with rotation " + 
//						Point.nf.format(c.getRotation() / Math.PI * 180.0));
				}
			}
			
			iCookies[i] = bestCookie;//.flushBottomToY(0).flushLeftToX(0);
			
			System.out.println("Returning cookie with rotation " + 
				Util.nf().format(bestCookie.getRotation() / Math.PI * 180.0) + 
				", width " + Util.nf().format(bestCookie.getBounds().getWidth()) +
				"\n" + bestCookie);
		}

	}

}
