Clipping with closed paths and trying to get open paths in the solution using the Clipper library with c#

南楼画角 提交于 2019-12-13 05:24:56

问题


I am using the c# version of Clipper. I would like to clip a closed subj path with a closed clip path, but have the result be multiple open lines. For example, slicing say a star shape into two parts with a long rectangle, but what is left of the two sides of the subj shape is two open lines.

In this clipping question, Angus said that the lines to be clipped must be open in order for the results to be open. Is there a way to do the clipping operation with two closed paths? As a workaround, I was thinking I could do a simple difference of the two closed paths and then traverse the result. Each time I find a vertex not contained in the subj path, then start a new path with the next vert. When I get to the end, join the first path to the end of the last path found. Is there another way?


回答1:


Paths in the Clipper library may be open or closed. You simply indicate whether the supplied paths are open or closed through the Closed parameter in the Clipper object's AddPath method. If all the paths are closed then you can be assured that all the paths in the clipping solution will be closed too.

      Path s = new Path();
      s.Add(new IntPoint(10, 40));
      s.Add(new IntPoint(40, 40));
      s.Add(new IntPoint(50, 10));
      s.Add(new IntPoint(60, 40));
      s.Add(new IntPoint(90, 40));
      s.Add(new IntPoint(65, 60));
      s.Add(new IntPoint(75, 90));
      s.Add(new IntPoint(50, 70));
      s.Add(new IntPoint(25, 90));
      s.Add(new IntPoint(35, 60));

      Path c = new Path();
      c.Add(new IntPoint(49, 0));
      c.Add(new IntPoint(51, 0));
      c.Add(new IntPoint(51, 100));
      c.Add(new IntPoint(49, 100));

      Paths solution = new Paths();
      Clipper cpr = new Clipper();
      cpr.AddPath(s, PolyType.ptSubject, true);
      cpr.AddPath(c, PolyType.ptClip, true);
      cpr.Execute(ClipType.ctDifference, solution, PolyFillType.pftEvenOdd, PolyFillType.pftEvenOdd);                   



回答2:


A better workaround is to convert them into open paths and appending the first vertex. You can then handle them as open paths while they still signify the same polyline. This way you don't have the overhead of traversing the polygons again.



来源:https://stackoverflow.com/questions/27512293/clipping-with-closed-paths-and-trying-to-get-open-paths-in-the-solution-using-th

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