/* * CSE 303, Autumn 2006, Assignment 3, Problem 2 * * You must modify this program as described in the assignment. */ #include #include // Bounding Box Type: // Assumes top < bottom and left < right. typedef struct _BBox { double left; double top; double right; double bottom; } BBox; // Return the maximum of two numbers double max(double a, double b) { return (a > b) ? a : b; } // Return the minimum of two numbers double min(double a, double b) { return (a < b) ? a : b; } // Initialize the allocated BBox structure with the given values. void BBoxInit(BBox *bb, double left, double top, double right, double bottom) { bb->left = left; bb->top = top; bb->right = right; bb->bottom = bottom; } // Print the given BBox to stdout. void BBoxPrint(BBox *bb) { printf("[(%g,%g) (%g,%g)]\n",bb->left, bb->top, bb->right, bb->bottom); } /* PUT YOUR FUNCTIONS HERE */ BBox BBoxUnion1(BBox bb1, BBox bb2) { BBox box; box.left = min(bb1.left, bb2.left); box.top = min(bb1.top, bb2.top); box.right = max(bb1.right, bb2.right); box.bottom = max(bb1.bottom, bb2.bottom); return box; } BBox *BBoxUnion2(BBox *pbb1, BBox *pbb2) { BBox *pBox = malloc(sizeof(BBox)); pBox->left = min(pbb1->left, pbb2->left); pBox->top = min(pbb1->top, pbb2->top); pBox->right = max(pbb1->right, pbb2->right); pBox->bottom = max(pbb1->bottom, pbb2->bottom); return pBox; } BBox *BBoxUnion3(BBox *pbb1, BBox *pbb2) { BBox *pBox = malloc(sizeof(BBox)); *pBox = BBoxUnion1(*pbb1, *pbb2); return pBox; } void BBoxUnion4(BBox *pbbDest, BBox *pbb1, BBox *pbb2) { *pbbDest = BBoxUnion1(*pbb1, *pbb2); } void BBoxTest() { BBox boxes[11]; BBoxInit(&boxes[0], 0.0, 0.0, 0.0, 0.0); BBoxInit(&boxes[1], -4.0, -8.0, 0.0, 0.0); BBoxInit(&boxes[2], 0.0, 0.0, 5.0, 10.0); BBoxInit(&boxes[3], -3.0, -3.0, 1.0, 1.0); BBoxInit(&boxes[4], -1.0, -1.0, 6, 6); // Test BBoxUnion1 boxes[5] = BBoxUnion1(boxes[0], boxes[1]); boxes[6] = BBoxUnion1(boxes[1], boxes[2]); boxes[7] = BBoxUnion1(boxes[3], boxes[4]); // Test BBoxUnion2 BBox *newBB1 = BBoxUnion2(&boxes[0], &boxes[1]); BBox *newBB2 = BBoxUnion2(&boxes[1], &boxes[2]); BBox *newBB3 = BBoxUnion2(&boxes[3], &boxes[4]); // Test BBoxUnion3 BBox *newBB4 = BBoxUnion3(&boxes[0], &boxes[1]); BBox *newBB5 = BBoxUnion3(&boxes[1], &boxes[2]); BBox *newBB6 = BBoxUnion3(&boxes[3], &boxes[4]); // Test BBoxUnion4 BBoxUnion4(&boxes[8], &boxes[0], &boxes[1]); BBoxUnion4(&boxes[9], &boxes[1], &boxes[2]); BBoxUnion4(&boxes[10], &boxes[3], &boxes[4]); int i; for (i=0; i<11; i++) BBoxPrint(&boxes[i]); BBoxPrint(newBB1); BBoxPrint(newBB2); BBoxPrint(newBB3); BBoxPrint(newBB4); BBoxPrint(newBB5); BBoxPrint(newBB6); } int main(int argc, char**argv) { BBoxTest(); return 0; }