# Strategies in finding a rectilinear polygon within a polygon in GIS

## What is a Rectilinear polygon?

A rectilinear polygon, according to Wikipedia, is a polygon all of whose edge intersections are at right angles. Thus the interior angle at each vertex is either 90° or 270°.

## Use case for a Rectilinear polygon

The use case is for the user to beautify the shape of a polygon for procedural modelling as designing architecture is much easier using a regular-shaped land parcel. In my previous article, I explored similar outcome by finding the largest rectangle within a convex polygon. If that interests you, you can read the article here.

## Rules for the algorithm

- Form a rectilinear polygon without adding additional points.
- Rectilinear polygon must be within the original polygon
- As it is done using GIS, the inaccuracy of the angle should be at most 1% away from 90°/270°

## Greedy Algorithm

Stage 1: Expanding Options Stage

- Loop through the points to determine the state of each point based on its angle. Angles (270° < x < 360°) will need the help of its neighbor to make it 90°/270°.
- Expand on the possible options to make by determining whether the point or one of it’s neighbor should move to make the current corner become 90°/270°.

Stage 2: Execution Stage

- Based on the possible options explored in stage 1, loop through the points and move points and its dependencies.

**Strategies**

- Move along the lines

For angles that are 0° < x < 90°, we could shift the point along both of its lines until it form 90°. This can move the point while not affecting its’ neighbor’s angle by shifting along line connecting to it. Please see the images below for illustration.

2. Two-stage movement

In stage 1 —Expanding Options, some points will be marked to assist its neighbor to make them 90°/270°. After the algorithm has determined the dependency sequence, the dependee can perform a double move to first make its dependent 90°/270°, followed by itself. However, it may not always be possible for the point to make the second movement . In such case, the algorithm will assign its following point as its dependee.

The first movement is made by finding the 3rd point in a right-angle triangle, A, B and angle of corner A. Extend this line and try to intersect with the next line in the polygon. If an intersection can be found, we can move the dependent to the intersection. Next, we try to move along this line which will keep the dependee’s angle while making itself 90°/270°. This may not always be possible and when this happens, the algorithm will conclude its action and make its other neighbor the dependee. Please see the images below for illustration.

## Results

I have tested my on several simple land parcel. Green polygon is the input and red polygon is the output. Results so far looks reasonable.

## Conclusion

I have tested the algorithm on several existing land parcel. You can find its results below. I can think of a few scenario which the algorithm definitely will fail. As most land parcels are likely to in regular shape so as to attract developers, this simple method should work most of the time.