Create a polygon around a polyline like a buffer

a 夏天 提交于 2019-12-19 03:55:37

问题


I have looked around to find an example of how to take a polyline and create a buffer around it so I end up with a polygon.

So far I found out I need Minkowskis Sums to do so, but I can't get my head around to raw algorithm and translate that in to code.

I would prefer an example in C# or a walkthrough of the algorithm.


回答1:


You could use the OffsetPolygons() function in the Clipper library, but first you'd need to convert the polyline into a polygon. Do this by appending to the polyline a reverse copy of the polyline. But since duplicate vertices aren't allowed the reverse copy must exclude the first and last vertices: v1,v2,...,vn, v(n-1),...,v2.




回答2:


Here's a sample way to do something like this with the 2D objects already available with the .NET Framework, based off of this link

http://www.charlespetzold.com/blog/2008/04/Rounded-Graphics-in-WPF.html

  // ...
  StreamGeometry geom = new StreamGeometry();

  DrawLines(geom);

  Pen p = new Pen(Brushes.Black, 10);
  p.LineJoin = PenLineJoin.Round;
  p.EndLineCap = PenLineCap.Round;
  p.StartLineCap = PenLineCap.Round;


  PathGeometry pathGeomWide = geom.GetWidenedPathGeometry(p);
  PathGeometry pathGeom = pathGeomWide.GetOutlinedPathGeometry();

  Path myPath = new Path();
  myPath.Stroke = Brushes.Black;
  myPath.Data = pathGeom;
  myCanvas.Children.Add(myPath);
  // ...

private static void DrawLines(StreamGeometry geom)
{
  using (var context = geom.Open())
  {
    context.BeginFigure(new Point(20, 20), false, true);
    context.LineTo(new Point(100, 20), true, true);
    context.LineTo(new Point(100, 100), true, true);
    context.LineTo(new Point(200, 100), true, true);
  }
}




回答3:


Have you tried using the 'Dot Spatial' library from Codeplex?

http://dotspatial.codeplex.com/

That uses Geos & Proj4 internally, which already contain all the functionality you need (Most of the worlds GIS servers & Products are built on these 2 code-bases!)

Failing that, you could use SQlite:

http://sqlite.phxsoftware.com/

and Spatialite:

http://www.gaia-gis.it/spatialite/

Then using ADO.NET code in C# you can use simple GIS SQL Queries to perform your processing EG:

SELECT AsText(ST_Buffer(polyline,0.25),4326)

WHich will return a string something like:

MULTIPOLYGON((x y, x y, x y, x y......))

That you can then just parse.

No need to re-invent the wheel, when everything you need is readily available.



来源:https://stackoverflow.com/questions/8226330/create-a-polygon-around-a-polyline-like-a-buffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!