Clarification on SimplePolygons()

  • Preet

    Preet - 2013-02-28


    I'm trying to clean up some self-intersecting polygons by splitting them up into multiple simple polygons using Clipper. The results I get with SimplifyPolygons() seems to indicate that clipper allows vertices with the same position. See (

    Is there any way to force Clipper to return separate polygons as opposed to the above behaviour in the general case? I noticed that I could run SimplifyPolygons twice, once with a positive filltype and once with a negative filltype and this seemed to give me polygons separately, but I don't know if thats reliable in the general case.

  • Angus Johnson

    Angus Johnson - 2013-02-28

    Hi Preet.

    Is there any way to force Clipper to return separate polygons as opposed to the above behaviour in the general case?

    Not without modifying the underlying code.
    In the IntersectEdges() method, change the follow from ...

    if ( e1Contributing && e2contributing )
      if ( e1stops || e2stops || 
        (e1Wc != 0 && e1Wc != 1) || (e2Wc != 0 && e2Wc != 1) ||
        (e1->polyType != e2->polyType && m_ClipType != ctXor) )
          AddLocalMaxPoly(e1, e2, pt); 
          DoBothEdges( e1, e2, pt );

    to ...

    if ( e1Contributing && e2contributing )
        AddLocalMaxPoly(e1, e2, pt); 
        AddLocalMinPoly(e1, e2, pt);

    and I think that you'll get separate polygons at these points of intersection (though I haven't tested this).

    Last edit: Angus Johnson 2013-02-28
  • Preet

    Preet - 2013-02-28

    Hi Angus,

    This seems to work great! Thank you for the quick reply and example. I have a follow up question. In some cases, the input polygon can't be decomposed any further, but it still has non adjacent duplicate points... see for example. Can Clipper detect these cases?

  • Angus Johnson

    Angus Johnson - 2013-03-01

    Can Clipper detect these cases?

    Detect for what purpose?

    Would you prefer an inner polygon touching an outer polygon instead? I believe that for everyone who wants that, there is at least another who prefers the current implementation.

  • Timo Kähkönen

    Timo Kähkönen - 2013-04-23

    I tested the suggestion by changing the lines that were provided in the comment at, but there comes "ProcessHorizontal error" and the output has only one polygon, although there should be two separate ones.

    My Clipper version is Javascript Clipper (with the above changes) and the code that produced error:

    var cpr = new ClipperLib.Clipper();
    var weak = [{"X":100,"Y":200},{"X":150,"Y":150},{"X":100,"Y":100},{"X":200,"Y":100},{"X":150,"Y":150},{"X":200,"Y":200}];
    // console.log(JSON.stringify(weak));
    var simple = cpr.SimplifyPolygon(weak);
    // console.log(JSON.stringify(simple));

    The example polygon has one weak duplicate point: {"X":150,"Y":150}.

    The test is at:

    Have anyone tested that solution in C# or C++?

    Last edit: Timo Kähkönen 2013-04-23
  • Timo Kähkönen

    Timo Kähkönen - 2013-04-23

    Here is a more complex "simple" polygon (after simplifying in Clipper).

    All the points that seem to be together are exactly identical, but there are not duplicate sequential points nor self-intersections on face level, but self-intersections are on vertex level.

    To divide this to truly simple polygons, would require it to be divided into pieces.

    My eyes say that here is four bottom polygons and one (bigger) upper polygon that has a hole, so after truly-simplifying there should be totally five outer polygons and one hole. On a second look it seems that here is only 1 outer polygon with 5 hole polygons.

    The polygon can be seen as SVG at The points of the polygon are:


    How this like truly simplifying could be achieved using Clipper? If not in the current implementation, is it possible to modify the code in some way and how this could be done?

    Last edit: Timo Kähkönen 2013-04-24

Log in to post a comment.