#include <math.h>
#include "svg.h"

/* L-system -- Inkscape notation 
	
	A-F : draw
	G-L : move (without drawing)
	M-Z : do nothing
	| : turn 180-degrees
	+ : turn left
	- : turn right
	[ : push current position (and angle)
	] : pop position (and angle)  
 */

int color;
int dcolor = 1;

void interpL(int n, char *str, double length, double leftAngle, double rightAngle, char *table[]) {
    char c;
	if (str==0) return;
	for (; *str; str++) {
		c = *str;
		switch (c) {
		case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
		if (n==0) {
			forward(length);
			stroke(color = rotateH360(color, dcolor));
		} else {
			interpL(n-1, table[c], length, leftAngle, rightAngle, table);
		}
		break;
		case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
		if (n==0) {
			penUp(); forward(length); penDown();
		} else {
			interpL(n-1, table[c], length, leftAngle, rightAngle, table);
		}
		break;
		case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
		case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
		case 'Y': case 'Z':
		if (n>0) {
			interpL(n-1, table[c], length, leftAngle, rightAngle, table);
		}
		break;
		case '|': turn(180); break;
		case '+': turn(leftAngle); break;
		case '-': turn(rightAngle); break;
		case '[': pushTurtle(); break;
		case ']': popTurtle(); break;
		}
	}
}

char* table[] = { 0 };

int main(void) {

	
    start();
	rulers();
	stroke(color = hsb360(0, 100, 100));
    strokeWeight(0.5);
	/* penrose tiling 
	table['M'] = "OA++PA----NA[-OA----MA]++";
	table['N'] = "+OA--PA[---MA--NA]+";
	table['O'] = "-MA++NA[+++OA++PA]-";
	table['P'] = "--OA++++MA[+PA++++NA]--NA";
	table['A'] = "";
    interpL(5, "[N]++[N]++[N]++[N]++[N]", 10, 36, -36, table);
	*/
	table['F'] = "FF-F-";
	interpL(6, "F", 6, 90, -90, table);
    finish();
    return 0;
}
