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

#include "svg.h"

double startX, startY, width, height;
int cols, rows;

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

void f10a4_1(void) { CardSpec(14, 11, 91, 55, 2, 5); }
void f10a4_2(void) { CardSpec(18.6, 21.2, 86.4, 50.8, 2, 5); }
void f8a4_5(void)  { CardSpec(8, 10.5, 97, 69, 2, 4); }

void init(void) {
}

// 追加１：弾幕を背景に描画
void barrage_01(int ellipseNum, int sizeX, int sizeY) {
	int i;
	for(i=0; i<=ellipseNum; i++) {
		double x, y, h;
		x = randomInRange(10, 90);
		y = randomInRange(10, 60);
		h = randomInRange(0, 1);
		fill(hsb1(h, 1, 1));
		ellipse(x, y, sizeX, sizeY);
	}
}
// 第一引数：弾幕数
// 第二引数：横方向の楕円半径
// 第三引数：縦方向の楕円半径

// 追加２：バブルを背景に描画
void bubble_01(int bubbleNum, int bubbleSize) {
	double x, y, h;
	int i, j;
	strokeOpacity(0.4);
	strokeWeight(1);
	for (i=bubbleSize; i<bubbleSize+bubbleNum; i++){
		for (j=bubbleSize; j<bubbleSize+bubbleNum; j++){
			x = randomInRange(10, 90);
			y = randomInRange(10, 60);
			h = randomInRange(0, 1);
			stroke(hsb1(h, 1, 1));
			ellipse(x, y, j, i);
		}
	}
}
// 第一引数：バブル数（指定した数の自乗となる）
// 第二引数：バブルの規模（純粋な規模）

// 追加３：螺旋状に広がる正方形を背景に描画
void spiralRect_01(int rectNum, int rectSize, int translatePosi, int useRadians) {
	int i, angle = 24, x = 2;

	for (i=0; i<rectNum; i++) {
		stroke(hsb1(i/29.0, 1, 1));

		pushMatrix();

		if ( useRadians ) {
			rotate(radians(i*angle));
		} else {
			rotate(radians(-i*angle));
		}
		translate(i+translatePosi, translatePosi);

		rect(-5, -5, rectSize, rectSize);
		popMatrix();
	}
}
// 第一引数：正方形数
// 第二引数：正方形のサイズ（１辺のサイズ）
// 第三引数：広がり方（あいまいな指定となる）
// 第四引数：回転方向（true⇒時計周り、false⇒反時計回り）

// 追加４：星群を背景に描画する
void starSet_01(int starNum, int starPosi) {
	int i, angle = 8, x = -1;
  int countRadians = 0;
      
  for (i=0; i<starNum ;i++) {
      stroke(hsb1(i/29.0, 1, 1));
      rotate(radians(i*angle));
      countRadians += i*angle;
	
      beginShape();
      vertex(starPosi, starPosi-20);
      vertex(starPosi-12, starPosi+15);
      vertex(starPosi+18, starPosi-8);
      vertex(starPosi-18, starPosi-8);
      vertex(starPosi+12, starPosi+15);
      endShape(1);
	}
  rotate(radians(-countRadians));  // 角度を元に戻す
}
// 第一引数：星数
// 第二引数：広がり方（あいまいな指定となる）

// 追加５：正方形を指定した間隔で並べ背景に描画
void rectNet_02(double width, double height, int rectNumX, int rectNumY, int rectStaPosi, int rectDis) {
	int i, j, angle = 24, x = 2;
	double rectSizeX = width/rectNumX, rectSizeY = height/rectNumY;

	for (i=0; i<rectNumY; i++) {
	for (j=0; j<rectNumX; j++) {
		stroke(hsb1(i/29.0, 1, 1));

		pushMatrix();
        
		rect(j*rectSizeX+rectStaPosi, i*rectSizeY+rectStaPosi, rectSizeX-rectDis, rectSizeY-rectDis);
		popMatrix();
	}
	}
}
// 第一引数：名刺の横の長さ
// 第二引数：名刺の縦の長さ
// 第三引数：正方形の横の長さ
// 第四引数：正方形の縦の長さ
// 第五引数：正方形の描画開始位置
// 第六引数：正方形間の距離

// 追加６：横線を背景に描画
void horizontalLine_01(double width, double height, int rectNumY) {
	int i, angle = 24, x = 2;
	double rectSizeY = height/rectNumY;

	for (i=0; i<rectNumY; i++) {
		stroke(hsb1(i/29.0, 1, 1));

		pushMatrix();
        
		line(0, i*rectSizeY, width, i*rectSizeY);
		popMatrix();
	}
}
// 第一引数：名刺の横の長さ
// 第二引数：名刺の縦の長さ
// 第四引数：横線と横線の距離
// ※この関数は、追加５の rectNet_01 を元にして作成している。

// 追加７：縦線を背景に描画
void verticalLine_01(double width, double height, int rectNumX) {
	int i, angle = 24, x = 2;
	double rectSizeX = width/rectNumX;

	for (i=0; i<rectNumX; i++) {
		stroke(hsb1(i/29.0, 1, 1));

		pushMatrix();
        
		line(i*rectSizeX, 0, i*rectSizeX, height);
		popMatrix();
	}
}
// 第一引数：名刺の横の長さ
// 第二引数：名刺の縦の長さ
// 第四引数：縦線と縦線の距離
// ※この関数は、追加５の rectNet_01 を元にして作成している。

// 追加８：サインカーブを背景に描画
void sineCurve_01(int waveScale, int hypotenuse, int curveNum, int curvePosi, int curveDis) {
	int i, j;
	double radian, height;

	strokeWeight(1);

	for (i=0; i<curveNum; i++) {
		for (j=0; j<98; j++) {
			radian = (((waveScale*(double)j)*PI)/180);
			height = (sin(radian)*hypotenuse);
			stroke(hsb1(j/29.0, 1, 1));
			line(j, height+curvePosi, j, height+curvePosi);
		}
		curvePosi += curveDis;
      }
}
// 第一引数：角度の規模
// 第二引数：斜辺の長さ
// 第三引数：曲線数
// 第四引数：曲線の描画位置
// 第五引数：曲線間の距離

void drawFrame(void) {
	strokeWeight(0.1);
	stroke(bw1(0));
	noFill();
	rect(0, 0, width, height);
}

void drawCard(int n, int i, int j, double ratio) {
	drawFrame();  // for debug, comment out
	noFill();
	noStroke();
	
	// ここに追加する
//	barrage_01(10, 1, 1);						// 弾幕を背景に描画
//	bubble_01(3, 10);							// バブルを背景に描画
//		spiralRect_01(50, 10, 0, 1);				// 螺旋状に広がる正方形を背景に描画
		starSet_01(10, 50);							// 星群を背景に描画
//		rectNet_02(width, height, 10, 10, 5, 5);	// 正方形を指定した間隔で並べ背景に描画
//		horizontalLine_01(width, height, 10);		// 横線を背景に描画
//		verticalLine_01(width, height, 10);			// 縦線を背景に描画
//		sineCurve_01(3, 3, n, 50, 2);				// サインカーブを背景に描画

	noStroke();		
	fill(hsb360(ratio*360, 100, 100));
	textFont("MS-Mincho", 8);
	text("讃岐　太郎", 10, 12);
	fill(hsb360(ratio*360+30, 100, 100));
	textFont("MS-Mincho", 6);
	text("香川大学工学部", 10, 20);
	fill(hsb360(ratio*360+60, 100, 100));
	textFont("Times New Roman", 8);
	fill(hsb360(ratio*360+90, 100, 100));
	text("Taro Sanuki", 10, 34);
	textFont("Times New Roman", 5);
	fill(hsb360(ratio*360+120, 100, 100));
	text("Faculty of Eng., Kagawa Univ.", 10, 40);
	fill(hsb360(ratio*360+150, 100, 100));
	textFont("Courier New", 5);
	text("sanuki@eng.kagawa-u.ac.jp", 10, 50);
		
/*
	textFont("Arial Unicode MS", 20);
	fill(hsb360(ratio*360+180, 100, 100));
	pushMatrix();
	translate(width-20, 20);
	rotate360(ratio*360);
	translate(-10, 7.5);
	text("&#x1f43c;", 0, 0);
	popMatrix();
*/
}

int main(void) {
	int i, j, n=0;
	double x;

	a4Portrait();
	start();
	init();
	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();

  return 0;
}

