#include <math.h>
#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 drawFrame(void) {
  strokeWeight(0.1);
  stroke(bw1(0));
  noFill();
  rect(0, 0, width, height);
}

void drawCard(int n, int i, int j, double ratio) {
  int t;
  double d = 0.25;
  drawFrame();  // for debug, comment out

  noStroke();
  fill(hsb360(ratio*360, 100, 100));
  textFont("MS-Mincho", 8);
  text("讃岐 太郎", 10, 30);

  textFont("Times New Roman", 8);
  fill(hsb360(ratio*360+90, 100, 100));
  text("Taro Sanuki", 10, 50);
  noFill();
  pushMatrix();
  translate(width-25, 25);
  strokeWeight(1);
  for (t=0; t<128; t++) {
    double x0 = 15*cos((i+2) * 2 * PI * t / 128), 
           y0 = 15*sin((j+2) * 2 * PI * t / 128 + d),
           x1 = 15*cos((i+2) * 2 * PI * (t+1) / 128), 
           y1 = 15*sin((j+2) * 2 * PI * (t+1) / 128 + d);
    stroke(hsb360(t/128.0*720, 100, 100));
    line(x0, y0, x1, y1);
  }
  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;
}
