AngusJohnson / Clipper2

Polygon Clipping and Offsetting - C++, C# and Delphi
Boost Software License 1.0
1.34k stars 247 forks source link

About "RectClipLines", May be this is a bug??? #807

Closed jim-jiang-github closed 2 months ago

jim-jiang-github commented 3 months ago

C# project, Use nuget Clipper2 1.3.0, net6.0

S1: Code:

 static void Main(string[] args)
 {
     List<PointD> ps = new List<PointD>();
     ps.Add(new PointD(200, 1000));
     ps.Add(new PointD(3300, 1000));
     ps.Add(new PointD(3300, 600));
     ps.Add(new PointD(200, 600));
     ps.Add(new PointD(200, 1000));
     PathsD psd = new PathsD();
     psd.Add(new PathD(ps));
     double width = 15000;
     double height = 1520;
     int split = 500;
     SvgWriter svg = new(FillRule.NonZero);

     for (int i = 0; i < (int)(width / split); i++)
     {
         var startX = i * split;
         var endX = split * i + split;
         RectD clipRect = new RectD(Math.Max(0, startX - 2), 0, endX, height);
         PathsD paths = Clipper.RectClipLines(clipRect, psd, 0);
         SvgUtils.AddOpenSubject(svg, paths);
     }
     svg.SaveToFile("rectclip.svg");
 }

result: rectclip

S2: Code:

static void Main(string[] args)
{
    List<PointD> ps = new List<PointD>();
    ps.Add(new PointD(200, 1000));
    for (int i = 1; i < 32; i++)
    {
        ps.Add(new PointD(i * 100 + 200, 1000));
    }
    ps.Add(new PointD(3300, 1000));
    ps.Add(new PointD(3300, 600));
    for (int i = 1; i < 32; i++)
    {
        ps.Add(new PointD(3300 - i * 100, 600));
    }
    ps.Add(new PointD(200, 600));
    ps.Add(new PointD(200, 1000));
    PathsD psd = new PathsD();
    psd.Add(new PathD(ps));
    double width = 15000;
    double height = 1520;
    int split = 500;
    SvgWriter svg = new(FillRule.NonZero);

    for (int i = 0; i < (int)(width / split); i++)
    {
        var startX = i * split;
        var endX = split * i + split;
        RectD clipRect = new RectD(Math.Max(0, startX - 2), 0, endX, height);
        PathsD paths = Clipper.RectClipLines(clipRect, psd, 0);
        SvgUtils.AddOpenSubject(svg, paths);
    }
    svg.SaveToFile("rectclip.svg");
}

result: rectclip Demo: Clipper2Lib.Test.zip

I wanted to implement polygon cutting. I constructed a rectangle as a special polygon, and then I found that the result of code 1 was not what I wanted. What I wanted was the result of code 2.

The difference between the two codes is that the number of points describing the rectangle is different, but the result of the two codes describing the polygon is the same, but the code 2 fits more meaningless point data.

What is causing the two results to be different? Or am I doing it the wrong way? I want to cut any polygon, and it seems that most polygons are cut fine, except this rectangle.