import static util.Common.hsb360;
import static util.SVGUtil.*;

public class CardTree {
	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 init() {
	}

	private static void drawFrame() {
		strokeWeight(0.1);
		stroke(bw1(0));
		noFill();
		rect(0, 0, width, height);
	}

	private static double[][] branches = 
		{ { 1,      0.7,       0}     // 幹
		, { 0.45,   0.5,      -80}    // 左の枝
		, { 0.55,   0.45,      75} }; // 右の枝

	private static void generate(double ratio, int d, double x, double y, double len, double angle) {
		double x1, y1;

		if (d<=0) return;
		int color = hsb360(ratio*360 + d*7.5, 100, 100);     // ★　色相
		stroke(color);
		x1 = x + len * cos360(angle);
		y1 = y + len * sin360(angle);
		line(x, y, x1, y1);

		for (double[] branch : branches) {
			double r = branch[0];
			generate(ratio, d-1,
					x * (1 - r) + x1 * r, y * (1 - r) + y1 * r,
					len * branch[1], angle + branch[2]);
		}
	}

	private static void drawCard(int n, int i, int j, double ratio) {
		double delta = 7.5;                                 // ★　色相の変化量
		drawFrame();  // for debug, comment out

		noStroke();
		strokeOpacity(1);
		
		fill(hsb360(ratio*360-delta, 100, 100));            // ★　色の指定
		textFont("MS-Mincho", 8);
		text("讃岐　太郎", 10, 20);
		
		fill(hsb360(ratio*360-delta*2, 100, 100));          // ★　色の指定
		textFont("MS-Mincho", 6);	                       
		text("さぬき　たろう", 10, 35); 

		fill(hsb360(ratio*360-delta*3, 100, 100));          // ★　色の指定
		textFont("Times New Roman", 6);
		text("Taro Sanuki", 10, 50);

		noFill();
		pushMatrix();
		translate(width-40, 25);
		strokeWeight(1);
		strokeOpacity(0.5);
		branches[0][2] = 20 * ratio - 10;
		generate(ratio, 5 /* 再帰の深さ */, 0, 30, 18, -60);
		popMatrix();
	}

	public static void main(String[] args) {
		int i, j, n=0;
		double x; 
		a4Portrait();
		start();
		init();

		// ★　以下の３つのうち、どれか一つを選ぶ
//		CardSpec(14, 11, 91, 55, 2, 5);         // A-one F10A4-1 フォーマット
//		CardSpec(18.6, 21.2, 86.4, 50.8, 2, 5); // A-one F10A4-2 フォーマット
		CardSpec(8, 10.5, 97, 69, 2, 4);        // A-one F8A4-5  フォーマット

		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();
	}
}
