// ver09 2013.10.30
// ver08より、サインカーブを背景に描画する機能を追加した

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

public class CardFourier {
	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 double coeffs[] = {1, 0, 1.0 / 3, 0, 1.0 / 5, 0, 1.0 / 7, 0, 1.0 / 9, 0, 1.0 / 11, 0, 1.0 / 13, 0, 1.0 / 15, 0}; // ★　矩形波
	private static double coeffs[] = {1, 1.0 / 2, 1.0 / 3, 1.0 / 4, 1.0 / 5, 1.0 / 6, 1.0 / 7, 1.0 / 8};                            // ★　鋸歯波


	// 曲線を背景に描画
	private static void fourier(int n, double ratio) {
		int i, k = 120;
		double radian, y, y0;

		pushMatrix();
		translate(width * 0.1, height * 0.75);
		y0 = 0;
		for (i = 1; i < k; i++) {
			int j;
			radian = i * 5 * PI/k;
			y = 0;
			for (j = 0; j < n + 1; j++) {
			     y += coeffs[j]  *  Math.sin((j + 1) * radian);
			}
			stroke(hsb1((double)i / k * 5 + ratio, 1, 1));
			line((i-1) * width * 0.8 / k, y0 * height / 10, i * width * 0.8 / k, y * height / 10);
			y0 = y;
		}
		popMatrix();
	}


	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;                                    // ★　色相の変化量
		drawFrame();                                           // ★　調整用の枠線、印刷するときは、この行をコメントアウトする。

		strokeWeight(1);
		strokeOpacity(0.5);                                    // ★　曲線の色の透明度
		fourier(n, ratio);						               // 曲線を背景に描画

		noStroke();
		fill(hsb360(ratio * 360, 100, 100));                     // ★　色の指定
		textFont("MS-Mincho", 8);                              // 日本語部分のフォント　MS明朝 8ポイント
		text("讃岐 太郎", 10, 20);

		fill(hsb360(ratio * 360 + delta * 1, 100, 100));             // ★　色の指定
		textFont("MS-Mincho", 6);                              // 日本語部分のフォント　MS明朝 6ポイント
		text("香川大学創造工学部", 10, 28);

		fill(hsb360(ratio * 360 + delta * 2, 100, 100));             // ★　色の指定
		textFont("Times New Roman", 8);	                       // 英字部分のフォント Times New Roman 8ポイント
		text("Taro Sanuki", 10, 38); 

		fill(hsb360(ratio * 360 + delta * 3, 100, 100));             // ★　色の指定
		textFont("Times New Roman", 5);	                       // 英字部分のフォント Times New Roman 5ポイント
		text("Fac. of Eng. and Design, Kagawa Univ.", 10, 46);

		fill(hsb360(ratio * 360 + delta * 4, 100, 100));             // ★　色の指定
		textFont("Courier New", 5);                            // メールアドレス用の等幅フォント（通常変更しない）
		text("sanuki.taro@kagawa-u.ac.jp", 10, 52);

	}

	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();
	}
}
