# PolygonsFromLines 1.0.0

.NET standard polynomial-time algorithm to create a set of smallest polygons (or cycles) from any given set of 2D floating point lines

Install-Package PolygonsFromLines -Version 1.0.0

dotnet add package PolygonsFromLines --version 1.0.0

<PackageReference Include="PolygonsFromLines" Version="1.0.0" />

paket add PolygonsFromLines --version 1.0.0

## PolygonsFromLines

C# .NET standard polynomial-time algorithm to create a set of smallest polygons (or cycles) from any given set of 2D floating point lines. It is meant to work with euclidean distance as edge weight but theoretically works with other weights as well.

Smallest in this context means that no polygons are created that contain any smaller polygons. For example, take the following set of lines `((A,B),(A,C),(B,D),(C,D),(E,F),(G,H))`

:

```
A---E---B
| | |
G-------H
| | |
C---F---D
```

The algorithm will detect intersection point `I`

, where `(E,F)`

and `(G,H)`

intersect. The output will be the following set of polygons: `((A,E,G,I),(E,B,I,H),(G,I,C,F),(I,H,F,D))`

. As you can see, this is the set of smallest polygons in the given set of lines, because any larger polygons (like `(A,B,C,D)`

) are not included.

### Usage

For any set of lines you can call `PolygonConstructor.ConstructFromLines`

. If you know the given set of lines do not intersect, you can set `skipIntersectionRemoval`

to `true`

.

If you have a planar graph (a graph without intersecting edges) you can call `PolygonConstructor.ConstructFromGraph`

directly and skip the non-optimal intersection and graph construction algorithms.

### The algorithm

The algorithm consist of three main steps:

- Remove any intersection points from the given set of lines.
- Create a planar graph from the non-intersecting lines.
- Find the polygons in the graph.

Note that step 1 and 2 are not the main focus of this project and run in O(|E|<sup>2</sup>) and O(|V| ⋅ |E| + |V|<sup>2</sup>) respectively.

The main idea of the polygon-finding algorithm is that each edge can be part of at most 2 polygons. Simply count how many times each edge is visited and remove any edge from the graph that is visited 2 times. The algorithm consists of the following steps:

- Cleanup loose ends in the graph (vertices with < 2 edges, since those can never be part of a cycle)
- Sort the verices from top-left to bottom-right. This is needed such that the algorithm start at an vertex lying on the edge of the graph.
- While there are more than 2 vertices in the graph, do:
- Start vertex: Take the first vertex of the sorted vertices in the graph.
- Sort that vertex' neighbours from top-left to bottom-right.
- End vertex: Take the first vertex from the vertex' neighbours.
- Remove the edge between the start and end vertex. This edge can be safely deleted since we know that we took 2 vertices that lie on the edge of the graph, thus the edge lies on the edge of the graph and will only be part of 1 cycle. Furthermore, it needs to be deleted for the following step.
- Find the shortest path between the start and end vertex using Dijkstra's algorithm.
- The path that is found is a cycle and therefore we can add it as a polygon.
- Increase the counters of all edges in the path by 1.
- Delete any edges that are visited 2 times.
- Cleanup loose ends in the graph (vertices with < 2 edges, since those can never be part of a cycle)

- Return the set of generated polygons.

## PolygonsFromLines

C# .NET standard polynomial-time algorithm to create a set of smallest polygons (or cycles) from any given set of 2D floating point lines. It is meant to work with euclidean distance as edge weight but theoretically works with other weights as well.

Smallest in this context means that no polygons are created that contain any smaller polygons. For example, take the following set of lines `((A,B),(A,C),(B,D),(C,D),(E,F),(G,H))`

:

```
A---E---B
| | |
G-------H
| | |
C---F---D
```

The algorithm will detect intersection point `I`

, where `(E,F)`

and `(G,H)`

intersect. The output will be the following set of polygons: `((A,E,G,I),(E,B,I,H),(G,I,C,F),(I,H,F,D))`

. As you can see, this is the set of smallest polygons in the given set of lines, because any larger polygons (like `(A,B,C,D)`

) are not included.

### Usage

For any set of lines you can call `PolygonConstructor.ConstructFromLines`

. If you know the given set of lines do not intersect, you can set `skipIntersectionRemoval`

to `true`

.

If you have a planar graph (a graph without intersecting edges) you can call `PolygonConstructor.ConstructFromGraph`

directly and skip the non-optimal intersection and graph construction algorithms.

### The algorithm

The algorithm consist of three main steps:

- Remove any intersection points from the given set of lines.
- Create a planar graph from the non-intersecting lines.
- Find the polygons in the graph.

Note that step 1 and 2 are not the main focus of this project and run in O(|E|<sup>2</sup>) and O(|V| ⋅ |E| + |V|<sup>2</sup>) respectively.

The main idea of the polygon-finding algorithm is that each edge can be part of at most 2 polygons. Simply count how many times each edge is visited and remove any edge from the graph that is visited 2 times. The algorithm consists of the following steps:

- Cleanup loose ends in the graph (vertices with < 2 edges, since those can never be part of a cycle)
- Sort the verices from top-left to bottom-right. This is needed such that the algorithm start at an vertex lying on the edge of the graph.
- While there are more than 2 vertices in the graph, do:
- Start vertex: Take the first vertex of the sorted vertices in the graph.
- Sort that vertex' neighbours from top-left to bottom-right.
- End vertex: Take the first vertex from the vertex' neighbours.
- Remove the edge between the start and end vertex. This edge can be safely deleted since we know that we took 2 vertices that lie on the edge of the graph, thus the edge lies on the edge of the graph and will only be part of 1 cycle. Furthermore, it needs to be deleted for the following step.
- Find the shortest path between the start and end vertex using Dijkstra's algorithm.
- The path that is found is a cycle and therefore we can add it as a polygon.
- Increase the counters of all edges in the path by 1.
- Delete any edges that are visited 2 times.
- Cleanup loose ends in the graph (vertices with < 2 edges, since those can never be part of a cycle)

- Return the set of generated polygons.

## Dependencies

This package has no dependencies.

## GitHub Usage

This package is not used by any popular GitHub repositories.

## Version History

Version | Downloads | Last updated | ||
---|---|---|---|---|

1.0.0 | 69 | 9/30/2019 |