Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // change the list of boxes so that the coverage is the same but none overlaps
- // also filters out empty/invalid boxes
- // TODO: something better than O(n^2)
- void removeOverlappingAreas()
- {
- box2i[] filtered;
- for(int i = 0; i < cast(int)(boxes.length); ++i)
- {
- box2i A = boxes[i];
- assert(A.isSorted());
- // empty boxes aren't kept
- if (A.volume() <= 0)
- continue;
- bool foundIntersection = false;
- // test A against all other rectangles, if it pass, it is pushed
- for(int j = i + 1; j < cast(int)(boxes.length); ++j)
- {
- box2i B = boxes[j];
- box2i C = A.intersection(B);
- bool doesIntersect = C.isSorted() && C.volume() != 0;
- if (doesIntersect)
- {
- // case 1: A contains B, B is simply removed, no intersection considered
- if (A.contains(B))
- continue;
- foundIntersection = true; // A will not be pushed as is
- if (B.contains(A))
- break; // nothing from A is kept
- else
- {
- // Make 4 boxes that are A without C (C is contained in A)
- // Some may be empty though since C and touch 2 edges
- // General case
- // +---------+ +---------+
- // | A | | D |
- // | +---+ | After split +--+---+--+
- // | | C | | => | E| |F | At least two of D, E, F or G are empty
- // | +---+ | +--+---+--+
- // | | | G |
- // +---------+ +---------+
- D = box2i(A.min.x, A.min.y, A.max.x, C.min.y);
- E = box2i(A.min.x, C.min.y, C.min.x, C.max.y);
- F = box2i(C.min.x, C.min.y, C.max.x, C.max.y);
- G = box2i(A.min.x, C.max.y, A.max.x, A.max.y);
- if (D.volume() > 0)
- boxes ~= D;
- if (E.volume() > 0)
- boxes ~= D;
- if (F.volume() > 0)
- boxes ~= D;
- if (G.volume() > 0)
- boxes ~= D;
- }
- }
- }
- if (!foundIntersection)
- filtered ~= A;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement