# PolygonsFromLines 1.0.0

`dotnet add package PolygonsFromLines --version 1.0.0`
`NuGet\Install-Package PolygonsFromLines -Version 1.0.0`
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
`<PackageReference Include="PolygonsFromLines" Version="1.0.0" />`
For projects that support PackageReference, copy this XML node into the project file to reference the package.
`paket add PolygonsFromLines --version 1.0.0`
`#r "nuget: PolygonsFromLines, 1.0.0"`
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
```// Install PolygonsFromLines as a Cake Addin

// Install PolygonsFromLines as a Cake Tool
#tool nuget:?package=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:

1. Remove any intersection points from the given set of lines.
2. Create a planar graph from the non-intersecting lines.
3. 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.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed.
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed.
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed.
MonoAndroid monoandroid was computed.
MonoMac monomac was computed.
MonoTouch monotouch was computed.
Tizen tizen40 was computed.  tizen60 was computed.
Xamarin.iOS xamarinios was computed.
Xamarin.Mac xamarinmac was computed.
Xamarin.TVOS xamarintvos was computed.
Xamarin.WatchOS xamarinwatchos was computed.
Compatible target framework(s)
Included target framework(s) (in package)