Search code examples
c#.netclipperlib

Anything wrong with my code:"clipping polygon(ellipse points) with line segments(subject) with clipper library in c#"


1.I'm drawing an ellipse and lines on a PictureBox.
2.I'm getting Paths object count as zero after performing clipper.PolyTreeToPaths(polytree_solution);see the contents
3.PolyTree object contains this see the contents.

I know my question is similar to these links but I could not get solution to problem, may be I am unable to find wrong in the code. 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.I want to ct.Intersection lines(subject) and circle(clip) but that's not happening pic click here .
2.Actual drawing on a PictureBox click here


Solution

  • It's very difficult to determine where your error(s) is when you don't provide crucial elements of your code (specifically we have no indication of the coordinates of m_rectArena).

    Anyhow here's what I've cobbled together from the information you have provided (with an approximation of the ellipse seen in one of your attached images):

          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);
    

    And here's the result: enter image description here