1

I am looking for an algorithm in C# that will take a polyline (i.e. a line with nodes/corners) and, given a width/buffer, generate a polygon. In other words, if you picture a polyline with a certain width, I'd like to get the bounding polygon that fits around that polyline.

The polyline is simply a list of (x,y) coordinates which I'd like to input into the algorith, together with a certain width, and have it spit out a list of (x,y) coordinates describing the nodes/corners of the polygon.

So something like:

point[] PolylineToPolygon(Point[] points, double width)
{
    // algorithm
}

void Convert()
{
    Point[] linePoints = new Point[] { new Point { X = -25.125675, Y = 28.434342 }, new Point { X = -26.232687, Y = 29.958363 }, new Point { X = -24.554377, Y = 26.445767 } };
    point[] polygonPoints = PolylineToPolygon(linePoints, 0.003);
}

From what I've read, I need to use the Minkowski algorithm but I cannot find an implementation in C#, nor am I sure exactly which Minkowski algorithm to use...

Any help or pointers would be greatly appreciated!

Fabricio Rodriguez
  • 3,769
  • 11
  • 48
  • 101
  • Possible duplicate of [An effective algorithm for buffering a polyline to create a polygon?](https://stackoverflow.com/questions/487504/an-effective-algorithm-for-buffering-a-polyline-to-create-a-polygon) – Anders Forsgren Oct 31 '17 at 19:34
  • For "good" polylines you can just build bisectors in the nodes and calculate points lying on these bisectors at given distance from segments. But I'm afraid that for "bad" polylines (acute angles, almost-loops) you would need some robust library like Clipper http://www.angusj.com/delphi/clipper.php – MBo Oct 31 '17 at 20:05
  • Yeah, this problem gets surprisingly nasty. My particular problem space is roads. One thing to consider is you might not end up with full polygons (consider a low-width polyline that effectively forms a circle. The inside of that circle is not included in the polygon, making it not really a polygon.) – zzxyz Oct 31 '17 at 21:56

1 Answers1

4

You can use lib NetTopologySuite. See github.com/NetTopologySuite/NetTopologySuite Here you can buffer your polyline and the resulting geometry will be a polygon. Your code would be something like this:

LineString ls = new LineString(new Coordinate[] { new Coordinate { X = -25.125675, Y = 28.434342 }, new Coordinate { X = -26.232687, Y = 29.958363 }, new Coordinate { X = -24.554377, Y = 26.445767 } }); 
Polygon result = ls.Buffer(0.003) as Polygon; 

Btw: This is an implementation of Minkowski sum.

Ohlsen1980
  • 294
  • 5
  • 11