#include #include #include "descartes.h" int IsRectangleOK(point_t q, point_t r) { if ((XCoord(q) <= XCoord(r)) && (YCoord(q) <= YCoord(r))) { return 1; } else { return 0; } } rectangle_t Rectangle2(point_t p, point_t q) { point_t bl, tr; int x1 = XCoord(p); int x2 = XCoord(q); int y1 = YCoord(p); int y2 = YCoord(q); int minx, maxx, miny, maxy; if (x1 <= x2) { minx = x1; maxx = x2; } else { minx = x2; maxx = x1; } if (y1 <= y2) { miny = y1; maxy = y2; } else { miny = y2; maxy = y1; } bl = Point(minx, miny); tr = Point(maxx, maxy); return Rectangle(bl, tr); } int main (void) { point_t p = Point(40, 300); point_t q = Point(400, 100); point_t r = Point(50, 50); rectangle_t rect1, rect2, rect3; OpenGraphics(); printf("First example.\n"); printf("Corners of rect1 are initially (%d, %d) ", XCoord(p), YCoord(p)); printf("and (%d, %d).\n", XCoord(q), YCoord(q)); printf("The answer of IsRectangleOK for rect1 is %d.\n", IsRectangleOK(p,q)); printf("and the resulting Rectangle is\n"); FillRectangle(Rectangle(p,q)); printf("Click anywhere in the window to continue...\n"); GetPoint(); Clear(); rect1 = Rectangle2(p,q); FillRectangle(rect1); p = BottomLeft(rect1); q = TopRight(rect1); printf("After calling Rectangle2, and extracting corners ...\n"); printf("Corners of rect1 are now (%d, %d) ", XCoord(p), YCoord(p)); printf("and (%d, %d).\n", XCoord(q), YCoord(q)); printf("Now answer of IsRectangleOK for rect2 is %d.\n", IsRectangleOK(p,q)); FillRectangle(rect1); printf("Click anywhere in the window to continue...\n"); GetPoint(); Clear(); printf("\nSecond example.\n"); printf("Corners of rect2 are initially (%d, %d) ", XCoord(q), YCoord(q)); printf("and (%d, %d).\n", XCoord(r), YCoord(r)); printf("The answer of IsRectangleOK for rect2 is %d.\n", IsRectangleOK(q,r)); printf("and the resulting Rectangle is\n"); FillRectangle(Rectangle(q,r)); printf("Click anywhere in the window to continue...\n"); GetPoint(); Clear(); rect2 = Rectangle2(q,r); q = BottomLeft(rect2); r = TopRight(rect2); printf("After calling Rectangle2, and extracting corners ...\n"); printf("Corners of rect2 are now (%d, %d) ", XCoord(q), YCoord(q)); printf("and (%d, %d).\n", XCoord(r), YCoord(r)); printf("Now answer of IsRectangleOK for rect2 is %d.\n", IsRectangleOK(q,r)); FillRectangle(rect2); printf("Click anywhere in the window to continue...\n"); GetPoint(); Clear(); printf("\nThird example.\n"); printf("Corners of rect3 are initially (%d, %d) ", XCoord(q), YCoord(q)); printf("and (%d, %d).\n", XCoord(p), YCoord(p)); printf("The answer of IsRectangleOK for rect3 is %d.\n", IsRectangleOK(q,p)); printf("and the resulting Rectangle is\n"); FillRectangle(Rectangle(q,p)); printf("Click anywhere in the window to continue...\n"); GetPoint(); Clear(); rect3 = Rectangle2(q,p); q = BottomLeft(rect3); p = TopRight(rect3); printf("After calling Rectangle2, and extracting corners ...\n"); printf("Corners of rect3 are now (%d, %d) ", XCoord(q), YCoord(q)); printf("and (%d, %d).\n", XCoord(p), YCoord(p)); printf("Now answer of IsRectangleOK for rect3 is %d.\n", IsRectangleOK(q,p)); FillRectangle(rect3); CloseGraphics(); return EXIT_SUCCESS; }