import static util.Common.hsb360;
import static util.SVGUtil.*;

public class Polar {
	private static double startX, startY, width, height;
	private static int cols, rows;

	//== 極方程式を描画

	// x = r cos(θ) + cx
	// y = r sin(θ) + cy
	// r/t = 1 + sc sin(stc θ) + cc cos(ctc θ)

	// 何倍で表示するか t
	// sinの係数 sc
	// cosの係数 cc
	// sinのθの係数 stc
	// cosのθの係数 ctc

	// 荒さ (255の約数であることを推奨) d
	private static void polar(double t, double sc, double cc, int stc, int ctc, int d) {
		int i;
		int unit = (0xFF / d) + 1;
		int len = 6*unit;
		// 点座標
		double lx = (1.0 + cc)*t;
		double ly = 0;
		// 色
		int cr = 0xFF;
		int cg = 0x00;
		int cb = 0x00;

		for (i = 1; i <= len; i++) {
			// 極方程式
			double a = i * 2*PI / len;
			double tr = 1.0 + sc*sin(a*stc) + cc*cos(a*ctc);
			double nx = tr*cos(a)*t;
			double ny = tr*sin(a)*t;
			// 色 (RGB)
			stroke(rgb255(cr, cg, cb));
			// 描画
			line(lx, ly, nx, ny);
			// 座標更新
			lx = nx;
			ly = ny;
			// 色更新
			if (i % unit == 0) continue;
			switch (i / unit) {
			case 0: cg += d; break;
			case 1: cr -= d; break;
			case 2: cb += d; break;
			case 3: cg -= d; break;
			case 4: cr += d; break;
			case 5: cb -= d; break;
			}
		}
	}


	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 void drawCard(int n, int i, int j, double ratio) {
		int t;
		double d = 0.25;

		double sc = 0.1 * n;
		double cc = 0.1 * (rows * cols - n);
		double stc = 5;
		double ctc = 10;

		drawFrame();                                   // ★　調整用の枠線、印刷するときは、この行をコメントアウトする。

		noStroke();
		
		fill(hsb360(ratio*360, 100, 100));             // ★　色の指定
		textFont("MS-Mincho", 8);
		text("讃岐　太郎", 10, 20);
		
		fill(hsb360(ratio*360+15, 100, 100));          // ★　色の指定
		textFont("MS-Mincho", 6);	                       
		text("さぬき　たろう", 10, 35); 

		fill(hsb360(ratio*360+30, 100, 100));          // ★　色の指定
		textFont("Times New Roman", 4);
		text(String.format("r ＝ 1 ＋ %.2f sin(%.2f θ) ＋ %.2f cos(%.2f θ)", sc, stc, cc, ctc), 10, 50);
		
		noFill();
		pushMatrix();
		strokeWeight(1);
		translate(width*0.75, height*0.3333);
		polar(10.0, sc, cc, (int)stc, (int)ctc, 1);
		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();
	}
}
