#include "a2.h"
#include <stdio.h>

void testPointFunctions(void) {
	Point p1 = pointInit(2, 3);
	Point p2 = pointInit(3, 5);

	printf("p1: (%.2f, %.2f), p2: (%.2f, %.2f)\n",
		pointX(p1), pointY(p1), pointX(p2), pointY(p2));
	printf("%.2f\n", pointDistanceBetween(p1, p2));
	printf("%s\n", pointAreEqual(p2, p1) ? "true" : "false");
	printf("--\n");
}

void testRectangleFunctions(void) {
	Rectangle r1 = rectangleInit(pointInit(3, 3), 4, 3);
	Rectangle r2 = rectangleInit(pointInit(5, 1), 4, 3);
	Rectangle r3, r4;

	printf("r1: { x = %.2f, y = %.2f, w = %.2f, h = %.2f }\n", 
		pointX(rectangleCentre(r1)), pointY(rectangleCentre(r1)),
		rectangleWidth(r1), rectangleHeight(r1));
	printf("r2: { x = %.2f, y = %.2f, w = %.2f, h = %.2f }\n", 
		pointX(rectangleCentre(r2)), pointY(rectangleCentre(r2)),
		rectangleWidth(r2), rectangleHeight(r2));
	printf("r1's area: %.2f\n", rectangleArea(r1));
	printf("r1 == r2?: %s\n", rectangleAreEqual(r1, r2)
		? "true" : "false");
	printf("--\n");

	r3 = rectangleIntersection(r1, r2);
	r4 = rectangleUnion(r1, r2);

	printf("r1: { x = %.2f, y = %.2f, w = %.2f, h = %.2f }\n", 
		pointX(rectangleCentre(r1)), pointY(rectangleCentre(r1)),
		rectangleWidth(r1), rectangleHeight(r1));
	printf("r2: { x = %.2f, y = %.2f, w = %.2f, h = %.2f }\n", 
		pointX(rectangleCentre(r2)), pointY(rectangleCentre(r2)),
		rectangleWidth(r2), rectangleHeight(r2));
	printf("r3: { x = %.2f, y = %.2f, w = %.2f, h = %.2f }\n", 
		pointX(rectangleCentre(r3)), pointY(rectangleCentre(r3)),
		rectangleWidth(r3), rectangleHeight(r3));
	printf("r4: { x = %.2f, y = %.2f, w = %.2f, h = %.2f }\n", 
		pointX(rectangleCentre(r4)), pointY(rectangleCentre(r4)),
		rectangleWidth(r4), rectangleHeight(r4));
	printf("--\n");
}

void testRectangleListFunctions(void) {
	Rectangle r1 = rectangleInit(pointInit(3, 3), 4, 3);
	Rectangle r2 = rectangleInit(pointInit(5, 1), 4, 3);
	Rectangle r3 = rectangleInit(pointInit(5.5, 3.5), 4, 3);
	Rectangle r4 = rectangleIntersection(r1, r2);
	Rectangle r5 = rectangleUnion(r1, r2);
	Rectangle r6, r7;

	RectangleList rl1 = rectangleListInit();
	RectangleList rl2;
	int i, n;

	printf("rl1: { size = %d }\n", rectangleListSize(rl1));
	printf("--\n");

	rl1 = rectangleListAdd(rl1, r1);
	n = rectangleListSize(rl1);
	printf("rl1: { size = %d }\n", n);
	for (i = 0; i != n; i++) {
		Rectangle r = rectangleListGet(rl1, i);
		printf("r[%d]: { x = %.2f, y = %.2f, w = %.2f, h = %.2f }\n", i,
		pointX(rectangleCentre(r)), pointY(rectangleCentre(r)),
		rectangleWidth(r), rectangleHeight(r));
	}

	printf("--\n");
	rl1 = rectangleListAdd(rl1, r2);
	rl1 = rectangleListAdd(rl1, r3);
	n = rectangleListSize(rl1);
	printf("rl1: { size = %d }\n", n);
	for (i = 0; i != n; i++) {
		Rectangle r = rectangleListGet(rl1, i);
		printf("r[%d]: { x = %.2f, y = %.2f, w = %.2f, h = %.2f }\n", i,
		pointX(rectangleCentre(r)), pointY(rectangleCentre(r)),
		rectangleWidth(r), rectangleHeight(r));
	}
	printf("--\n");

	r6 = rectangleListIntersection(rl1);
	r7 = rectangleListUnion(rl1);
	printf("r6: { x = %.2f, y = %.2f, w = %.2f, h = %.2f }\n", 
		pointX(rectangleCentre(r6)), pointY(rectangleCentre(r6)),
		rectangleWidth(r6), rectangleHeight(r6));
	printf("r7: { x = %.2f, y = %.2f, w = %.2f, h = %.2f }\n", 
		pointX(rectangleCentre(r7)), pointY(rectangleCentre(r7)),
		rectangleWidth(r7), rectangleHeight(r7));
	printf("--\n");

	rl1 = rectangleListSet(rl1, r4, 1);
	n = rectangleListSize(rl1);
	printf("rl1: { size = %d }\n", n);
	for (i = 0; i != n; i++) {
		Rectangle r = rectangleListGet(rl1, i);
		printf("r[%d]: { x = %.2f, y = %.2f, w = %.2f, h = %.2f }\n", i,
		pointX(rectangleCentre(r)), pointY(rectangleCentre(r)),
		rectangleWidth(r), rectangleHeight(r));
	}
	printf("--\n");

	rl1 = rectangleListAdd(rl1, r5);
	rl1 = rectangleListAdd(rl1, r6);
	n = rectangleListSize(rl1);
	printf("rl1: { size = %d }\n", n);
	for (i = 0; i != n; i++) {
		Rectangle r = rectangleListGet(rl1, i);
		printf("r[%d]: { x = %.2f, y = %.2f, w = %.2f, h = %.2f }\n", i,
		pointX(rectangleCentre(r)), pointY(rectangleCentre(r)),
		rectangleWidth(r), rectangleHeight(r));
	}
	printf("--\n");
	
	rl2 = rectangleListSubList(rl1, 1, 3);
	n = rectangleListSize(rl2);
	printf("rl2: { size = %d }\n", n);
	for (i = 0; i != n; i++) {
		Rectangle r = rectangleListGet(rl2, i);
		printf("r[%d]: { x = %.2f, y = %.2f, w = %.2f, h = %.2f }\n", i,
		pointX(rectangleCentre(r)), pointY(rectangleCentre(r)),
		rectangleWidth(r), rectangleHeight(r));
	}
	printf("--\n");
}

int main(void) {
	testPointFunctions();
	testRectangleFunctions();
	testRectangleListFunctions();
	
	return 0;
}

