AS3 Polygon Clipper


I was looking for an open source polygon clipper library for AS3 to use on a commercial project.
I found a few ports of the General Polygon Clipper library (GPC) but it is only free for non-commercial projects.
After more searching around I found this excellent library called Clipper by Angus Johnson. It did not have an AS3 port so I made one using Alchemy to wrap the c++ code.

The SWC and source code can be found on github: https://github.com/Flassari/as3clipper

It is completely free and open source for both personal and commercial projects. Clipper uses the Boost Software License.

Supported clipping operations: difference, intersection, union and XOR.

Here’s an example of how to use the AS3 port after importing the Clipper.swc file:

import com.flassari.geom.Clipper;
import com.flassari.geom.ClipType;

var subjectPolygon:Array = [new Point(0, 0), new Point(200, 0), new Point(100, 200)];

var clipPolygon:Array = [new Point(0, 100), new Point(200, 100), new Point(300, 200)];

var resultPolygons:Array = Clipper.clipPolygon(subjectPolygon, clipPolygon, ClipType.DIFFERENCE);

Update
There is now an AS3 port available at https://github.com/ChrisDenham/PolygonClipper.AS3

Tags: , , , , , , , , , , , , , , , , , , ,

9 Responses to “AS3 Polygon Clipper”

  1. Olli Says:

    Hi!
    Excellent work with the wrapper. It was just what I needed. One observation though. The very first call to clipPolygon takes a very long time. Is there something that can be done about it?

  2. Flassari Says:

    It happens because it is initializing the C library. You can not get rid of it, but you CAN choose when it happens. Just make a bogus call to it in the loading part of your code, people shouldn’t notice it that much.

  3. Bog Imp Says:

    Hello,
    Can you add method clipPolygons that work with

    Polygons subjectPolygons

    it’s will be amazing, i found that Clipper by Angus Johnson has this functionality.

  4. Dasun Says:

    Thanks buddy. Thank you so much. This was the exact thing we have been searching for.

  5. Paul Says:

    Hi, just want to say a big thank you for this, its working fantastic for me.

    How much of the lib is converted to Alchemy? the SimplifyPolygons function looks really useful too.

  6. Flassari Says:

    All of the lib is converted to alchemy, but to use the functions one would have to write a c++ function to expose that functionality. I haven’t had time to update the project for a while, but anyone can fork it and add to it.

  7. Olli Says:

    Hello again!
    Now that the flascc has been released, have you thought about making a new version? Might be interesting to see if the new compiler produces faster code.

  8. Flassari Says:

    Hi Olli!
    Actually, Chris Denham has created a new port that is pure AS3, you can find it at https://github.com/ChrisDenham/PolygonClipper.AS3

    I’d recommend using that one since Adobe made flascc a “Premium Feature”.
    http://www.adobe.com/devnet/flashplayer/articles/premium-features-licensing-faq.html
    I will probably look into porting Clipper to flascc eventually, but until then I recommend you check out Chris’ port =)

  9. FlashIndonesia Says:

    Wow, thank you. This is what I need :D