Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mittalgovind/polygon-partition

Python code for partitioning rectilinear polygon in O(n) time complexity
https://github.com/mittalgovind/polygon-partition

graph-algorithms ipython-notebook polygon python3 rectangles

Last synced: 6 days ago
JSON representation

Python code for partitioning rectilinear polygon in O(n) time complexity

Awesome Lists containing this project

README

        

# Polygon-Partition
Python code for partitioning rectilinear polygon in O(n) time complexity

**Rectillinear Polygon :** A simple connected single-cyclic graph in R2, such that each of its edge is perpendicular or in-line with another one of its edge(s).

*__Method of Labelling the graph__* \
We take input as a rectillinear polygon from cursor keys, i.e., up(↑), left(←), and right (→). As input is read, the pointer proceeds forward and draws a rectillinear polygon with its trail. The labelling of the vertices starts from v0 to vn-1, and v0 = vn, where _n_ is the _number of vertices in the polygon_.


pic0

A Rectillinear polygon consisting of 20 vertices.

Pressing a key once means going forward, left, or right. A distance of only one unit can be traversed at a time.

#### INPUTS
**_G_** = Rectilinear Graph \
**_X_** = Set of Abscissa of vertices \
**_Y_** = Set of Ordinates of vertices \
**_Collinear\_Vertices_** = Set of Collinear Vertices \
**_Concave\_Vertices_** = Set of Concave Vertices \
**_Horizontal\_Chords_** = Set of Horizontal Chords \
**_Vertical\_Chords_** = Set of Vertical Chords

#### Important points to note

1. Left and Right operations changes the direction the pointer faces.
2. Vertices that are induced after going forward consecutively. Although in the example, they are not explicitly shown, but they do exist and at a distance of one unit from its previous vertex.
3. If the interior angle made by the two edges incident at this vertex is 270 degree.
4. Chords are lines joining two vertices which are not already part of the polygon.
5. As, the way of labelling is defined, there is unique labelling of each rectillinear polygon.

**EXAMPLE** \
In the above figure, the pointer is shown by an arrow. \
Total number of vertices = 20 \
Collinear\_Vertices = [v1, v2, v3, v9, v13, v17, v18, v19]
Concave\_Vertices = [v6, v7, v12, v14]

### _**Algorithm for Finding Maximum partitions**_

**Maximum Partition:** Partition of given rectillinear polygon into maximum number of non-overlapping rectangles.

##### STEP I

```python
max_partition(G):
for u in Concave_Vertices:
for v in Concave_Vertices and v > u+1:
if exists a chord joining v & u and ~exists another concave
vertex on chord joining v & u:
if chord is horizontal:
add (v, u) to Horizontal_Chords
else if chord is vertical:
add (v, u) to Vertical_Chords
else :
loop_back
```

**_Task Achieved:_** All the edges that exist between *any two concave vertices* are being added to their *respectful categories*. \

**EXAMPLE** \
pic1

*Horizontal\_Chords* = ∅ \
*Vertical\_Chords* = [(v7, v12)]

*Explanation*:
**u > v :** Comparison between two vertices is done on the basis of their respective vertex indices. \
Here **_v-u_** should be greater than unity, because this assures the vertex v is not consecutive to u and has a higher index than u. Thus, iteration through each pair of vertex is done only once, making it more efficient. \

In the above code, we iterate through all (concave vertex, concave vertex') pairs, and check for existence of vertical and horizontal chords, that are not intersected by any other vertex. \
We observe that, v7 and v12 are the only two concave vertices and between whom, there exists a vertical chord. Therefore, it is added to the set of *Vertical\_Chords*. Also, there does not exist any horizontal chord between any two concave vertices and therefore, set of *Horizontal\_Chords* is empty. \

##### STEP II

```python
for u in Collinear_Vertices:
for v in Concave_Vertices:
if exists a chord joining v & u and ~exists another concave
or collinear vertex on chord joining v & u:
if chord is horizontal:
add (v, u) to Horizontal_Chords
else if chord is vertical:
add (v, u) to Vertical_Chords
else :
loop_back
```

**_Task Achieved:_** All the chords between *collinear vertices and concave vertices* are being added to their *respective categories*.

**EXAMPLE**

pic2

*Horizontal\_Chords* = [(v9, v12), (v17, v14), (v18, v7), (v19, v6)] \
*Vertical_Chords* = [(v7, v12), (v1, v4), (v3, v6)]

*Explanation*:
In the above code, we iterate through all (collinear vertex, concave vertex) pairs, and check for existence of vertical and horizontal chords between them, that are not intersected by any other vertex. \
If any chord is found, it is added to set of *Vertical\_Chords* or *Horizontal\_Chords*, depending on its orientation. \

##### STEP III

Thus, we have found all the chords, and only need to plot them now.
```python
plot(X,Y)
plot(Horizontal_Chords)
plot(Vertical_Chords)
display(plot)
```

test 0 2

##### STEP IV

Now we have found the maximum partition, but to find the minimum partition the following needs to be done

1. Find a maximum independent set of chords (i.e., a maximum cardinality set of independent chords).
2. Draw the chords in this maximum independent set. This partitions the polygon into smaller rectilinear polygons.

##### STEP V
From each of the concave vertices from which a chord was not drawn in *Step IV* draw a maximum length vertical line that is wholly within the smaller rectilinear polygon created in *Step III* that contains this vertex.

##### STEP VI

Thus, we have found all the chords, and only need to plot them now.
```python
plot(X,Y)
plot(Horizontal_Chords)
plot(Vertical_Chords)
plot(Nearest_Partial_Chords)
display(plot)
```
*Test Case 1*
test1 1
test 1 2

*Test Case 2*
2 1
2 2

*Test Case 3*
3 1
3 2

*Test Case 4*
test 4 1
test 4 2