How to get minimum count rectangles that covers another pile of rectangle? - c++

Assume I have a pile of rectangles, some of which intersect, some isolate. E. g.
+--------------- + +-------- +
| | | |
| | | |
| A | | C |
| +---------------- + | |
| | | | +---------+-------- +
| | | | | |
+---------|----- + B | | D |
| | | |
| | +------------------ +
+---------------- +
+------------------ + +-------- +
| | | |
| E | | X |
+-------------------+ | |
| | +-------- +
| | +------------ +
| | | |
| F | | |
| | | Y |
| | | |
+-------------------+ +------------ +
Rect A, B intersect with each other, C, D have one same point, E, F have two same points, X, Y are isolated.
I have two questions:
How to partion these rectangles into rectangles which cover A, B, C, D, E, F, X, Y exactly also have minimum count like this:
+---------+----- + +-------- +
| | | | |
| | | | |
| | | | |
| | +--------- + | |
| | | | +---------+-------- +
| | | | | |
+---------+ | | | |
| | | | |
| | | +-------------------+
+------+----------+
+------------------ + +-------- +
| | | |
| | | |
| | | |
| | +---------+
| | +------------ +
| | | |
| | | |
| | | |
| | | |
+-------------------+ +-------------+
How to cover intersected rectangles with big ones? Like this:
+---------------------------+ +-------------------+
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | +-------------------+
+---------------------------+
+-------------------+ +---------+
| | | |
| | | |
| | | |
| | +---------+
| | +------------ +
| | | |
| | | |
| | | |
| | | |
+-------------------+ +-------------+
For Q1, I've no idea at all....
For Q2, I wrote some code in C++ but have poor efficiency. I believe there're better methods/algorithm.
bool intersectRect(const Rect& rect1, const Rect& rect2) {
/* if rect1 and rect2 intersect return true
else return false
*/
}
Rect mergeIntersectRects(const set<Rect>& rects) {
// suppose rects are all intersected. This function only return a smallest Rect that cover all rects.
}
set<Rect> mergeRectToRects(const set<Rect>& rectset, const Rect& rect) {
set<Rect> _intersect_rects;
set<Rect> _unintersect_rects;
for(set<Rect>::const_iterator it = rectset.begin();
it != rectset.end();
++it) {
if(intersectRect(*it, rect))
_intersect_rects.insert(*it);
else
_unintersect_rects.insert(*it);
}
if(!_intersect_rects.empty()) {
_intersect_rects.insert(rect);
return mergeRectToRects(_unintersect_rects,
mergeIntersectRects(_intersect_rects));
}
else {
_unintersect_rects.insert(rect);
return _unintersect_rects;
}
}

First, I'm assuming that your rectangles are all axis-aligned.
For Q1, one option would be to sweep the plane while maintaining a list of line segments along the sweep line that lie in the interior of the rectangles. As you discover each rectangle vertex during the sweep you can check to see if it modifies the current interior segments and if so, start or end a rectangle as necessary.
For example, let's say your sweep line moves left to right:
Current
Interior
|
+-|------------- + +-------- + *
| | | | | |
| | | | | |
| | A | | C | |
| | +---------------- + | | |
| | | | | +---------+-------- + |
| | | | | | | |
+-|-------|----- + B | | D | *
| | | | |
| | | +------------------ +
| +---------------- +
|
+-|---------------- + +-------- + *
| | | | | |
| | E | | X | |
| |-----------------+ | | |
| | | +-------- + |
| | | +------------ + |
| | | | | |
| | F | | | |
| | | | Y | |
| | | | | |
+-|-----------------+ +------------ + *
|
When the sweep line is in the position shown above, there are two interior segments. Namely, that inside A and that inside (E U F). When the sweep line reaches the leftmost edge of B, we output a rectangle for the portion of A lying to the left. We then replace the interior of A in the segment list with the interior of (A U B).
Current
Interior
|
+---------+-|--- + +-------- + *
| | | | | | |
| | | | | | |
| | | | | C | |
| | |-------------- + | | |
| | | | | +---------+-------- + |
| | | | | | | |
+---------+ |--- + B | | D | |
| | | | | |
| | | +------------------ + |
+-|-------------- + *
|
+-----------|------ + +-------- + *
| | | | | |
| | | | X | |
| |-------+ | | |
| | | +-------- + |
| | | +------------ + |
| | | | | |
| | | | | |
| | | | Y | |
| | | | | |
+-----------|-------+ +------------ + *
|
For Q2, the answer could be computed during the same sweep by keeping track of the x-coordinate at which a segment was first added to the list (e.g. "left side of A") as well as the min and max y-coordinates that it spans during its lifetime (e.g. bottom of B to top of A). When the segment is finally removed from the list (e.g. "right side of B"), then output a rectangle using these four coordinates.
Sorting the rectangle vertices lexicographically in a preprocessing step would be O(n * log n). Processing them would be O(log n) since you can do a binary search on the known interior ranges. The total runtime should be O(n * log n).

Q1: this is called partition of rectilinear polygon. Answer from Rob's comment has very good description. I found paper mentioned in the answer useful.
Q2: I suppose that you don't want two covers of non-intersecting regions to intersect. Like cover for 3 rectangle, 2 rectangle producing L and rectangle intersection cover of L but not any L rectangle.
If it is like that, than it is possible to incrementally create covers. Here is a simple algorithm for it.
covers = {A}
for each rectangle R
while there is a cover that intersects R, say C
remove C from covers and set R = cover of R and C
add R to covers
This code is not efficient in standard form. With good structure for covers structure, it can be efficient.

Here's the algorithm: http://goo.gl/aWDJo
You can read about finding the convex hull algorithms: http://ralph.cs.cf.ac.uk/papers/Geometry/boxing.pdf

I'd use the method suggested by #Damon but speed up neighbouring rectangle search with some spatial indexing structure, for example a quadtree or a grid. You'd need two of them, first built over the set of input rectangles, to search for intersecting rectangles to split, and second built over the set of split rectangles obtained in first step, to search adjacent rectangles to merge. That should speed things up considerably compared to the naive approach.

Related

Is there a general set of instructions to swap adjacent and non-adjacent records in a linked list?`

With a singly linked list, swapping non-adjacent cells can be described by the following operation, assuming '=>' means 'now links to':
Y => X->next
X => Y->next
BeforeY => X
BeforeX => Y
However, this operation does not work for swapping adjacent records, creating circular links, as X->next (say) will be Y.
Adjacent record swaps, assuming X is before Y, can be described by a separate set of operations:
X => Y->next
Y => X
BeforeX => Y
I can't seem to solve this set of operations as a subset of the previous set or common children of a parent set.
Is there a uniform, unconditional set of operations that describes a swap that works for both adjacent and non-adjacent records?
Here is some ASCII Art for the two scenarios.
Non-adjacent X and Y
X can come before or after Y in the list as long as there is at least one element between X and Y (so Ax and By could identify the same node when X comes before Y). The situation before:
Bx X Ax By Y Ay
+----+ +----+ +----+ +----+ +----+ +----+
| | | | | | | | | | | |
-->| |-->| |-->| |--...->| |-->| |-->| |-->
| | | | | | | | | | | |
+----+ +----+ +----+ +----+ +----+ +----+
The situation after:
Bx X Ax By Y Ay
+------------------------------+
| |
+------------------------------+ |
| | | |
+----+ | +----+ | +----+ +----+ | +----+ | +----+
| |-+ | |-+ | | | | +>| | +>| |
-->| #| | #| | |--...->| #| | #| | |-->
| | +>| | +>| | | |-+ | |-+ | |
+----+ | +----+ | +----+ +----+ | +----+ | +----+
| | | |
+------------------------------+ |
| |
+------------------------------+
The 4 'next' pointers marked with # are changed.
Before:
Bx->next = X
X->next = Ax
By->next = Y
Y->next = Ay
After:
Bx->next = Y
Y->next = Ax
By->next = X
X->next = Ay
Adjacent X and Y
Assume X immediately precedes Y. The situation before:
Bx X Ax
By Y Ay
+----+ +----+ +----+ +----+
| | | | | | | |
-->| |---->| |---->| |---->| |-->
| | | | | | | |
+----+ +----+ +----+ +----+
The situation after:
Bx X Ax
By Y Ay
+------------+
| |
+----+ | +----+ | +----+ +----+
| |-+ | | +>| | | |
-->| #| | #| | #| | |-->
| | +>| |-+ | |-+ +>| |
+----+ | +----+ | +----+ | | +----+
| | | |
+-------------------+ |
| |
+------------+
Before:
Bx->next = X
X->next = Ax = Y
By->next = Y
Y->next = Ay
After:
Bx->next = Y
Y->next = X # Different
By->next = Ay # Different
X->next = Ay
The result values in the pointers are different, as shown. This means that there isn't a single mapping that works for both the "non-adjacent X and Y" and the "adjacent X and Y" cases — the pointer twizzling must be different.

difference between size_t (*B)[N] and size_t *B[N]

what's the difference between these two lines of code in c++?
size_t (*B)[N] = new size_t[N][N]; and
size_t *B[N] = new size_t[N][N];
first one compiles correctly but with second line, g++ gives this error
matrixim.cpp:43:20: error: array must be initialized with a brace-enclosed initializer
43 | size_t *B[N] = new size_t[N][N];
size_t *B[N]
Here, B is an array of N pointers to size_t
size_t (*B)[N]
Here, B is a pointer to an array of N size_ts
Both of these constructs could be used to create something approximating a 2-dimensional array, but their layout in memory is very different.
size_t *B[N] would look something like this:
B +-------------+-------------+-----+-------------+
+--------+ | B[0][0] | B[0][1] | ... | B[0][N-1] |
| B[0] +--->+-------------+-------------+-----+-------------+
+--------+
| B[1] +--->+-------------+-------------+-----+-------------+
+--------+ | B[1][0] | B[1][1] | ... | B[1][N-1] |
| | +-------------+-------------+-----+-------------+
| ... |
| |
+--------+
| B[N-1] +--->+-------------+-------------+-----+-------------+
+--------+ | B[N-1][0] | B[N-1][1] | ... | B[N-1][N-1] |
+-------------+-------------+-----+-------------+
B is an array of N pointers to size_t, each of which points to the first element of an array of N size_t.
size_t (*B)[N] would look something like this:
B +---------------------------------------------------+
+----+ | B[0] |
| +--->+ +-------------+-------------+-----+-------------+ |
+----+ | | B[0][0] | B[0][1] | ... | B[0][N-1] | |
| +-------------+-------------+-----+-------------+ |
+---------------------------------------------------+
| B[1] |
| +-------------+-------------+-----+-------------+ |
| | B[1][0] | B[1][1] | ... | B[1][N-1] | |
| +-------------+-------------+-----+-------------+ |
+---------------------------------------------------+
| |
| ... |
| |
+---------------------------------------------------+
| B[N-1] |
| +-------------+-------------+-----+-------------+ |
| | B[N-1][0] | B[N-1][1] | ... | B[N-1][N-1] | |
| +-------------+-------------+-----+-------------+ |
+---------------------------------------------------+
Here, B is a pointer to the first element of an array of N arrays of N size_t.

STL or ranges algorithm to efficiently find n consecutive elements that satisfy predicate

I have the following function(toy example, but good for demonstration):
// finds the iterator pointing to the start of n consectuve 47s or return values.end() if not found
auto find_n_47s(const int n, const std::vector<int>& values){
std::vector<bool> predicate_result;
predicate_result.reserve(values.size());
std::transform(values.begin(), values.end(), std::back_inserter(predicate_result), []
(const auto& val){return val==47; });
std::vector<bool> search_pattern(n, true);
auto it= std::search(predicate_result.begin(), predicate_result.end(),
search_pattern.begin(), search_pattern.end());
return values.begin() + std::distance(predicate_result.begin(), it);
}
I am looking for a nicer and more efficient way to accomplish the same thing.
My problems:
I can not use manual iteration + std::all_of(from current element to
n elements ahead) because it is too slow(in theory for every element
that I do up to n predicate applications).
My solution allocates memory and calculates predicate for every
element although we might find the result in the first 1% of the
elements.
Full code here: https://wandbox.org/permlink/rBVFS64IcOI6gKe6
As Cruz Jean pointed out you can use search_n:
https://wandbox.org/permlink/ENgEi5ZVPDx1D1GQ
The GCC implementation of search_n
https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/stl_algo.h
does not check every element:
|---------|---------|---------|----------------|
|  Find first occurence of n=7 consecutive 47  |
|---------|---------|---------|----------------|
|  vector |  step#  |  count  |            |
|---------|---------|---------|----------------|
|  47     |         |         |            |
|  47     |         |         |            |
|  47     |         |         |            |
|  0      |  4      |  3      |            |
|  47     |  3      |  3      |            |
|  47     |  2      |  2      |            |
|  47     |  1      |  1      |  start     |
|  47     |         |         |            |
|  47     |         |         |            |
|  0      |  5      |  0      |            |
|  47     |         |         |            |
|  0      |         |         |            |
|  0      |         |         |            |
|  47     |         |         |            |
|  0      |         |         |            |
|  47     |         |         |            |
|  0      |  6      |  0      |            |
|  0      |         |         |            |
|  0      |         |         |            |
|  0      |         |         |            |
|  0      |         |         |            |
|  47     |         |         |            |
|  0      |         |         |            |
|  0      |  7      |  0      |            |
|  47     |         |         |            |
|  47     |         |         |            |
|  47     |         |         |            |
|  47     |         |         |            |
|  0      |         |         |            |
|  0      |  9      |  1      |            |
|  47     |  8      |  1      |            |
|  47     |  15     |  7      |  success   |
|  47     |  14     |  6      |            |
|  47     |  13     |  5      |            |
|  47     |  12     |  4      |            |
|  47     |  11     |  3      |            |
|  47     |  10     |  2      |            |
|  47     |         |         |            |
|  0      |         |         |            |
|  0      |         |         |            |
|  47     |         |         |            |
|  0      |         |         |            |
|  …      |         |         |            |
|---------|---------|---------|----------------|

How to print a 6* 6 grid as described below in Python

How can i print a grid as shown below without any contents:
I tried the below input:
grid = [["|" for x in range(7)] for y in range(6)]
for row in grid:
print(" ".join(row))
But it generated the below output:
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
I can't think of how to connect the empty spaces with _. Any help would be appreciated.
try below code:
grid = [[" _" for x in range(6)]]
for y in range(6):
list1 = []
for x in range(13):
if x%2 == 0:
list1.append("|")
else:
list1.append("_")
grid.append(list1)
for row in grid:
print("".join(row))
output:
_ _ _ _ _ _
|_|_|_|_|_|_|
|_|_|_|_|_|_|
|_|_|_|_|_|_|
|_|_|_|_|_|_|
|_|_|_|_|_|_|
|_|_|_|_|_|_|

Union of rectangles in a 2d tiled world

I am trying to find the bounding polygon of a set of adjacent cells(row,col) ( convertable to rectangles) in a 2d tiled world.
Processing the cells in a for loop and using the neighbourhood property of the adjacent cells I could eliminate all internal edges and store the rest of the edges.
The edges are stored in std::vector;
Now I need to merge the edges where there is a common vertex and slope is the same.
After merging the edges I need to make the bounding polygon, starting from a vertex going counter clockwise.
Please help to find a method to make it possible.
I think this is a simple algorithm to achieve that.
Consider we have this as input:
| | | | | |
-+---+---+---+---+---+-
| | | | | |
-+---+---+---+---+---+-
| | | a | | |
-+---+---+---+---+---+-
| | b | c | d | |
-+---+---+---+---+---+-
| | | e | | |
-+---+---+---+---+---+-
| | | | | |
Where a, b, c, d, and e are our input tiles stored as a vector of pairs (Coordinates):
std::vector<std::pair<unsigned,unsigned>> tiles;
What we want is this:
| | | | | |
-+---+---+---+---+---+-
| | | | | |
-+---+---*---*---+---+-
| | | | | |
-+---*---* *---*---+-
| | | |
-+---*---* *---*---+-
| | | | | |
-+---+---*---*---+---+-
| | | | | |
The algorithm works as follows:
Build an array of booleans enclosing the entire set of tiles. You have to trasverse the set to find the bounds of that rectangle. Set as true the positions of the array which represent a tile of the set, and as false otherwise.
The output in the example will be (T is true and f is false):
+---+---+---+
| f | T | f |
+---+---+---+
| T | T | T |
+---+---+---+
| f | T | f ]
+---+---+---+
Now you have to traverse the border of the hull polygon. Start at the first element marked as true in the flag array and trasverse the vertices in the same direction until you reach the first vertex again, using this rules:
If the two tiles in front of the current direction/position are false, turn clockwise and add the vertex to the output list (polygon):
(* are vertices added to the polygon, X the current vertex, the arrow the current direction)
+---+---+---+
| f | f | f |
*---+---X---+ --->
| T | T | f |
*---+---+---+
| f | T | f ]
+---+---+---+
goes to
+---+---+---+
| f | f | f | |
*---+---*---+ |
| T | T | f | v
*---+---X---+
| f | T | f ]
+---+---+---+
If one tile is false and one true, go in the same direction (Note that true-false or false-true means you are in a border):
+---+---+---+
| f | f | f | |
*---+---*---+ |
| T | T | f | v
*---+---X---+
| f | T | f ]
+---+---+---+
goes to
+---+---+---+
| f | f | f | |
*---+---*---+ |
| T | T | f | v
*---+---+---+
| f | T | f ]
+---+---X---+
If both tiles are true, turn counter-clockwise and add the vertex to the output list (Note that true-true means you have reached part of the set of tiles, a "wall"):
+---+---+---+
| f | f | f | |
*---+---*---+ |
| T | T | f | v
*---+---X---+
| f | T | T ]
+---+---+---+
goes to
+---+---+---+
| f | f | f |
+---+---*---+
| T | T | f | --->
+---+---*---X
| f | T | T ]
+---+---+---+
Considerations:
tilemap coordinates vs flag-array coordinates
The flag-array represents the rectangle region of the tilemap where the tiles are placed. So the tilemap-coordinates of its first element (tile) is (left,top) where left is the minimum x-coordinate of the selected set of tiles, and top is the minimum y-coordinate of the selected set of tiles.
In the second step of the agorithm, you trasverse the frontier (border) of the set of tiles, using the array as a guide. Note that what you really trasverse is that array, so you have to translate the coordinates from flag-coordinates (logical coordinates) to tilemap-coordinates (physical coordinates) to store the vertices of the polygon. Of course thats easy.
Also note that the algorithm abstract steps trasverse vertices of edges (physical tile coordinates), not logical-coordinates. You have to be sure what "I'm in that vertex" means and what "advance" and "turn" mean in terms of flag-array coordinates.
Border conditions and the front-tiles check
We have defined three rules to advance along the border of the set of tiles. We have used the flag-array as a guide to decide what to do (Advance, turn clockwise, or turn counter-clockwise). Note that when the current vertex is in the border of the array, you could (you should) consider that it have neighbour tiles with a false value.
For example:
+---+---+---+
| f | f | f | |
*---+---*---+ |
| T | T | f | v
*---+---+---+
| f | T | f ]
+---+---X---+
goes to
+---+---+---+
| f | f | f |
*---+---*---+ <--
| T | T | f |
*---+---+---+
| f | T | f ]
+---X---*---+
exactly as if it was this:
+---+---+---+
| f | f | f | |
*---+---*---+ |
| T | T | f | v
*---+---+---+
| f | T | f ]
+---+---X---+
| f | f | f |
*---*---*---+
Possible optimisations
The first step computes the flag array because the algorithm takes the set of tiles selected as a vector. If your tile engine supports it, you could add a property to the tiles (bool selected) and pass the tilemap directly, avoiding the computation and the vertex cooordinates transformations.
Example
Given this flag-array:
+---+---+---+
| T | f | f |
+---+---+---+
| T | T | T |
+---+---+---+
| f | T | T |
+---+---+---+
The execution works as follows (Note that the drawings are the state AFTER the execution of the step):
Find the first true tile. In this case (0,0). So we start at one of its vertex (bottom-left vertex, looking upwards, for example. Note that because its the first true tile, you could use that vertex being sure that it belongs to the polygon. So add that first vertex to the polygon):
Current position: (0,1)
Polygon: {(0,1)}
+---+---+---+ ^
| T | f | f | |
X---+---+---+ |
| T | T | T |
+---+---+---+
| f | T | T |
+---+---+---+
Start the trasverse. In this case, the front tiles are false-true, so advance:
Current position: (0,0)
Polygon: {(0,1)}
X---+---+---+ ^
| T | f | f | |
*---+---+---+ |
| T | T | T |
+---+---+---+
| f | T | T |
+---+---+---+
The front tiles are false-false (We are in a border), so turn clockwise and add the vertex:
Current position: (1,0)
Polygon: {(0,1),(0,0)}
*---X---+---+
| T | f | f |
*---+---+---+
| T | T | T | --->
+---+---+---+
| f | T | T |
+---+---+---+
Now the fron-tiles are false-false (One is out of the array, and the other is false). turn clockwise and add the vertex:
Current position: (1,1)
Polygon: {(0,1),(0,0),(1,0)}
*---*---+---+
| T | f | f | |
*---X---+---+ |
| T | T | T | v
+---+---+---+
| f | T | T |
+---+---+---+
The two front-tiles are true: Turn counter-clockwise and add the vertex:
Current position: (1,2)
Polygon: {(0,1),(0,0),(1,0),(1,1)}
*---*---+---+
| T | f | f |
*---*---X---+
| T | T | T | --->
+---+---+---+
| f | T | T |
+---+---+---+
One tile is false and the other is true: Advance:
Current position: (1,3)
Polygon: {(0,1),(0,0),(1,0),(1,1)}
*---*---+---+
| T | f | f |
*---*---+---X
| T | T | T | --->
+---+---+---+
| f | T | T |
+---+---+---+
Two false tiles (Both out of array): Turn clockwise and add the vertex:
Current position: (2,3)
Polygon: {(0,1),(0,0),(1,0),(1,1),(3,1)}
*---*---+---+
| T | f | f | |
*---*---+---* |
| T | T | T | v
+---+---+---X
| f | T | T |
+---+---+---+
One true and one false(Out of array): Advance:
Current position: (3,3)
Polygon: {(0,1),(0,0),(1,0),(1,1),(3,1)}
*---*---+---+
| T | f | f | |
*---*---+---* |
| T | T | T | v
+---+---+---+
| f | T | T |
+---+---+---X
Two false(Out of array) front-tiles: Turn clockwise and add the vertex:
Current position: (2,3)
Polygon: {(0,1),(0,0),(1,0),(1,1),(3,1),(3,3)}
*---*---+---+
| T | f | f |
*---*---+---*
| T | T | T | <---
+---+---+---+
| f | T | T |
+---+---X---*
true-false (One true and one out of bounds): Advance:
Current position: (1,3)
Polygon: {(0,1),(0,0),(1,0),(1,1),(3,1),(3,3)}
*---*---+---+
| T | f | f |
*---*---+---*
| T | T | T | <---
+---+---+---+
| f | T | T |
+---X---+---*
false-false (One false and one out of bounds): Turn clockwise and add the vertex:
Current position: (1,2)
Polygon: {(0,1),(0,0),(1,0),(1,1),(3,1),(3,3),(1,3)}
*---*---+---+
| T | f | f | ^
*---*---+---* |
| T | T | T | |
+---X---+---+
| f | T | T |
+---*---+---*
true-true front-tiles: Turn counter-clockwise and add the vertex:
Current position: (0,2)
Polygon: {(0,1),(0,0),(1,0),(1,1),(3,1),(3,3),(1,3),(1,2)}
*---*---+---+
| T | f | f |
*---*---+---*
| T | T | T | <---
X---*---+---+
| f | T | T |
+---*---+---*
false-false front-tiles: Turn clockwise and add the vertex:
Current position: (0,1)
Polygon: {(0,1),(0,0),(1,0),(1,1),(3,1),(3,3),(1,3),(1,2),(0,2)}
*---*---+---+
| T | f | f | ^
X---*---+---* |
| T | T | T | |
*---*---+---+
| f | T | T |
+---*---+---*
The current vertex is the first vertex of the polygon: The execution have finished. The result is as follows:
Polygon: {(0,1),(0,0),(1,0),(1,1),(3,1),(3,3),(1,3),(1,2),(0,2)}
*---*
| |
* *-------*
| |
*---* |
| |
*-------*
Because of tiles, this is a 'special case' of finding boundary polygon. I would go with some simple approach.
Tile (x,y) consist of vertices [(x,y), (x+1,y), (x+1,y+1), (x,y+1)]. Vertex is a part of boundary polygon if it is in 1, 2 or 3 tiles. To find vertices on boundary it is enough to count number of tiles it is in. For that it is enough to pass through tiles and increment vertex appearance count for tile 4 vertices. Vertices with tile count 1, 2 or 3 are on the boundary polygon.
To order vertices it is enough to start from some vertex on a boundary and look for a neighbouring vertex that is also on a boundary. To traverse vertices in same direction it is important to take a care of direction order of neighbouring vertices to check. E.g. if last vertex is on -x, than order of directions to check is +y, +x, -y.
Since direction of last edge is known, if next edge is in the same direction than that vertex is for a removal. If region is simple connected, than removal can be known also from tile count. If tile count of a vertex is 2 than vertex is for removal.
For now we don't have guarantee that order is clockwise. That can be check by checking (some of) upper-most edge(s) is it in clockwise direction. If it is not, than reverse polygon.