import static util.Common.hsb360;
import static util.SVGUtil.*;

public class CardLissajous {
	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 int a[] = { 2,  2,  2,  3,  3,  3,  5,  5};  // ★　Lissajous 曲線のパラメーター
	private static int b[] = { 3,  5,  7,  5,  7, 11,  7, 11};  // ★　Lissajous 曲線のパラメーター

	private static void drawCard(int n, int i, int j, double ratio) {
		int t;
		double delta = 15;                            // ★　色相の変化量
		
		double d0 = randomInRange(0, 2 * PI);         // ★　適当に値を変える
		double d1 = randomInRange(0, 2 * PI);         // ★　適当に値を変える
		
		drawFrame();                                  // ★　調整用の枠線、印刷するときは、この行をコメントアウトする。

		noStroke();
		
		fill(hsb360(ratio*360, 100, 100));             // ★　色の指定
		textFont("MS-Mincho", 8);
		text("讃岐　太郎", 10, 20);
		
		fill(hsb360(ratio*360+delta, 100, 100));       // ★　色の指定
		textFont("MS-Mincho", 6);	                       
		text("さぬき　たろう", 10, 35); 

		fill(hsb360(ratio*360+delta*2, 100, 100));     // ★　色の指定
		textFont("Times New Roman", 6);
		text("Taro Sanuki", 10, 50);
		
		noFill();
		pushMatrix();
		translate(width-25, 30);
		strokeWeight(1);
		strokeOpacity(0.8);
		for (t=0; t<128; t++) {
			double x0 = 15*cos(a[n] * 2 * PI * t / 128 + d0), 
					y0 = 15*cos(b[n] * 2 * PI * t / 128 + d1),
					x1 = 15*cos(a[n] * 2 * PI * (t+1) / 128+ d0), 
					y1 = 15*cos(b[n] * 2 * PI * (t+1) / 128 + d1);
			stroke(hsb360(t/128.0*720, 100, 100));
			line(x0, y0, x1, y1);
		}
		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();
	}
}
