// cases 4-7 void test_disjoint_corner() { bool all_pass = false; rect_list_type cliprects; rect_type mainrect(40,40,20,20); rect_type ul(35,55,10,10); rect_type ur(55,55,10,10); rect_type ll(35,35,10,10); rect_type lr(55,35,10,10); // upper left cliprects = disjoint_union(mainrect, ul); rect_type ul_1(35, 55, 5, 5); rect_type ul_2(35, 60, 10, 5); all_pass = (cliprects.size() == 3) && rect_list_contains(cliprects, ul_1) && \ rect_list_contains(cliprects, ul_2) && rect_list_contains(cliprects, mainrect); if (!all_pass) { printf("Error in test_disjoint_corner()i: upper left\n"); } // lower left cliprects = disjoint_union(mainrect, ll); rect_type ll_1(35, 35, 10, 5); rect_type ll_2(35, 40, 5, 5); all_pass = (cliprects.size() == 3) && rect_list_contains(cliprects, ll_1) && \ rect_list_contains(cliprects, ll_2) && rect_list_contains(cliprects, mainrect); if (!all_pass) { printf("Error in test_disjoint_corner()i: upper left\n"); } // upper right cliprects = disjoint_union(mainrect, ur); rect_type ur_1(55, 60, 10, 5); rect_type ur_2(60, 55, 5, 5); all_pass = (cliprects.size() == 3) && rect_list_contains(cliprects, ur_1) && \ rect_list_contains(cliprects, ur_2) && rect_list_contains(cliprects, mainrect); if (!all_pass) { printf("Error in test_disjoint_corner()i: upper right\n"); } // lower right cliprects = disjoint_union(mainrect, lr); rect_type lr_1(55, 35, 10, 5); rect_type lr_2(60, 40, 5, 5); all_pass = (cliprects.size() == 3) && rect_list_contains(cliprects, lr_1) && \ rect_list_contains(cliprects, lr_2) && rect_list_contains(cliprects, mainrect); if (!all_pass) { printf("Error in test_disjoint_corner()i: lower right\n"); } }
set<Item>& set<Item>::intersect(const set<Item>& s1, const set<Item>& s2) { clear(); // sort s1, s2 s1.sort(); s2.sort(); // if s1 is below s2, return if (s1[s1.size()-1] < s2[0]) return *this; // compare every element of s1 with s2 for (int i=0; i<s1.sz; ++i) { // if s1 above s2, return if (s1[i] > s2[s2.size()-1]) return *this; if (s2.member(s1[i]) >= 0) disjoint_union(s1[i]); } sorted = s1.sorted; return *this; }
// cases 2 and 3 void test_disjoint_2_3() { rect_list_type cliprects; rect_type bigrect(10,10,80,80); rect_type smallrect(15,15,50,10); // case 2 cliprects = disjoint_union(bigrect, smallrect); if ((cliprects.size() != 1) || (cliprects[0] != bigrect)) { printf("Error in test_disjoint_2_3(): case 2.\n"); } // case 3 cliprects = disjoint_union(smallrect, bigrect); if ((cliprects.size() != 1) || (cliprects[0] != bigrect)) { printf("Error in test_disjoint_2_3(): case 3.\n"); } }
// case 1 void test_disjoint_outside() { rect_list_type cliprects; rect_type rect1(20,20,40,40); rect_type rect2(70,20,40,40); cliprects = disjoint_union(rect1, rect2); assert(cliprects.size() == 2); assert(rect_list_contains(cliprects, rect1)); assert(rect_list_contains(cliprects, rect2)); }
rect_list_type disjoint_union(const rect_list_type &rects) { if (rects.size() < 2) { return rects; } rect_list_type rlist; rlist.push_back(rects[0]); for (unsigned int i=1; i<rects.size(); i++) { rlist = disjoint_union(rlist, rects[i]); } return rlist; }
rect_list_type disjoint_union(const rect_type &a, const rect_type &b) { rect_list_type rlist; rlist.push_back(a); return disjoint_union(rlist, b); }