import static util.Common.hsb360;
import static util.SVGUtil.*;

public class BookCoverShiraga {
	private static double startX, startY, width, height;
	private static int cols, rows;

	private static void CardSpec(double sx, double sy, double dx, double dy, int c, int r) {
		startX = sx; startY = sy; width = dx; height = dy;
		cols = c; rows = r;
	}
	
	private static void CardSpec(double sx, double sy, int c, int r) {
		CardSpec(sx, sy, (pageWidth() - sx * 2) / c , (pageHeight() - sy * 2) / r, c, r);
	}

	private static void init() {
	}

	// 調整用の枠線を描く
	private static void drawFrame() {
		strokeWeight(0.1);
		stroke(bw1(0));
		noFill();
		rect(0, 0, width, height);
	}

	private static void drawCard(int n, int i, int j, double ratio) {
		double delta = 7.5;                                 // ★　色相の変化量
		int m, angle = 35;
		
//		drawFrame();                                        // ★　調整用の枠線、印刷するときは、この行をコメントアウトする。
		
		noStroke();

		//ベジエ
		noFill();
		pushMatrix();
		translate(width * 0.5, height * 0.45);                             // 移動
		scale(0.2, 0.2);                                                  // x, y 方向に縮小
		for(m = 0; m < 40 ;m++) {
			stroke(hsb360(m * (10 + n), m * (3 + i), m * (5 + j)));       // 線の色変更
			strokeWeight(0.75);
			pushMatrix();

			rotate360((m * 27) + angle);                                  // 指定した角度分回転
			translate(2, 0);                                              // x 方向に 2 移動

			// ベジエ曲線の描写
			// (0,0) から (40,40) まで 2 つの座標を通るような曲線を引く
			bezier(0, 0, 10 + m, - 2 * (j-2) - 2 * m, 
					    2 * (i-0) + 2 * m, 60 / (0.2 * (n-2) + 1) - 2 * m, 40, 40);
			popMatrix();
		}

		//アイコン
//		textFont("Arial Unicode MS", 20);
//		fill(hsb100(ratio*100+40, 100, 100));
//		translate(-10, 7.5);
//		text("&#x1f383;", 0, 0);                         // ★　中心に表示する絵文字

		popMatrix();
	}

	public static void main(String[] args) {
		int i, j, n=0;
		double x;
		
		start();
		rulers();
		init();
		
		// ★　以下の３つのうち、どれか一つを選ぶ
//		CardSpec(20, 28, 4, 3);  // A4 縦 3 x 横 4 の場合
		CardSpec(20, 28, 5, 4);    // A4 縦 4 x 横 5 の場合
//		CardSpec(20, 28, 6, 5);    // A4 縦 5 x 横 6 の場合

		x = startX;
		for (i=0; i<cols; i++) {    
			double y = startY;
			for (j=0; j<rows; j++, n++) {
				pushMatrix();
				translate(x, y);
				drawCard(n, i, j, (double)n/(cols*rows));
				popMatrix();
				y += height;
			}
			x += width;
		}
		finish();
	}
}
