我的代码有任何问题:“在C#中使用Clipper库剪切带有线段(主题)的多边形(椭圆点)”

时间:2019-03-29 13:50:08

标签: c# .net clipperlib

1。我正在PictureBox上绘制椭圆和线条。
2.执行clipper.PolyTreeToPaths(polytree_solution); see the contents
后,我得到Paths对象计数为零 3。PolyTree对象包含此see the contents

我知道我的问题与这些链接相似,但是我无法解决问题,可能是我无法在代码中找到错误。 link1 link2 link3

//adding subject (lines)
Paths subj=new Paths(1);
subj.Add(new Path());
subj[0].Add(new IntPoint(0,0));
subj[0].Add(new IntPoint(440,280));
subj[0].Add(new IntPoint(440,0));
subj[0].Add(new IntPoint(0,280));
subj[0].Add(new IntPoint(440/2,280));
subj[0].Add(new IntPoint(440/2,0));

//for clip, GraphicsPath object is defined properly by ellipse points by help of mouse events
GraphicsPath path=new GraphicsPath();
path.AddEllipse(m_rectArena);

Paths clip=new Paths(1);
int scale=100;
path.Flatten();
Path clip2=new Path(path.PointCount);
foreach(PointF p in path.PathPoints)
{
  clip2.Add(new IntPoint((int)(p.X*scale),(int)(p.Y*scale)));
}
clip.Add(clip2);
Paths solution=new Paths();
PolyTree polytree_solution=new PolyTree();
Clipper c=new Clipper();
c.AddPath(subj[0],PolyTree.ptSubject,false);
c.AddPaths(clip,PolyType.ptClip,true);
c.Execute(ClipType.ctIntersection,polytree_solution,PolyFilllType.pftEvenOdd,PolyFillType.pftEvenOdd);
Paths openp=Clipper.PolyTreeToPaths(polytree_solution);


1。我想ct.Intersection行(主题)和圆(剪辑),但是pic click here 并没有发生。
2.在PictureBox click here

上的实际绘图

1 个答案:

答案 0 :(得分:0)

当您不提供代码的关键元素时(特别是我们没有m_rectArena坐标的指示),很难确定错误在哪里。

无论如何,这就是我根据您提供的信息(在随附的一张图片中看到的椭圆近似值)拼凑而成的内容:

      Paths subj = new Paths(1);
      subj.Add(new Path());
      subj[0].Add(new IntPoint(0, 0));
      subj[0].Add(new IntPoint(440, 280));
      subj[0].Add(new IntPoint(440, 0));
      subj[0].Add(new IntPoint(0, 280));
      subj[0].Add(new IntPoint(440 / 2, 280));
      subj[0].Add(new IntPoint(440 / 2, 0));

      GraphicsPath gpEllipse = new GraphicsPath();
      Rectangle r = new Rectangle(40,40,360,360);
      gpEllipse.AddEllipse(r);
      gpEllipse.Flatten();

      Paths clip = new Paths(1);
      clip.Add(new Path());
      foreach (PointF p in gpEllipse.PathPoints)
        clip[0].Add(new IntPoint(p.X, p.Y));

      Clipper c = new Clipper();
      c.AddPaths(subj, PolyType.ptSubject, false);
      c.AddPaths(clip, PolyType.ptClip, true);
      Paths solution = new Paths();
      PolyTree solutiontree = new PolyTree();
      c.Execute(ClipType.ctIntersection, solutiontree, 
        PolyFillType.pftEvenOdd, PolyFillType.pftEvenOdd);
      solution = Clipper.OpenPathsFromPolyTree(solutiontree);

      //nb: you'll find SVGBuilder in SVG.cs in the sample code accompanying Clipper
      SVGBuilder svg = new SVGBuilder();
      svg.AddPaths(subj, Color.White, Color.Black, false);
      svg.AddPaths(clip, Color.FromArgb(0x18, 0xFF, 0xFF, 0), Color.Black, true);
      svg.PenWidth = 2.5;
      svg.AddPaths(solution, Color.White, Color.Red, false);
      svg.SaveToFile("c:\\temp\\test.svg", 640, 480, 20);

结果如下: enter image description here