Fanning Software Consulting

Clipping Polygons

QUESTION: I have two polygons and I need to clip these two polygons and make a single composite polygon consisting of the overlap. Is there a routine in IDL that can do this for me?

ANSWER: There is nothing like that built into IDL. However, clipping algorithms have been written in IDL. Matti Meron has written one such IDL program, named SHAPE_OVERLAP, that can be found in the ITTVIS User-Contributed Code Library. You will need to download his entire library, as the code depends upon other files in the library. (At the time of this writing [17 February 2007], there was a bug in the program that needed to be fixed before the program worked correctly. See the IDL Newsgroup thread for additional information. It is possible the file has been updated in the ITTVIS code library.)

Mark Hadfield has also implemented the Sutherland-Hodgeman polygon clipper algorithm in his Motley Library set of programs. See the program files MGH_POLYCLIP and MGH_POLYCLIP2.

C Implementation of Polygon Clipper

JD Smith adds this comment.

I notice you point to Mark Hadfield's PolyClip2. This routine is based on my original Sutherland-Hodgman clipper POLYCLIP, which I provided Mark (and he re-wrote). I'm now (finally) providing the recently improved clipper code on my webpage.

It's for clipping polygons against pixel grids, and includes a newly rewritten C implementation of the S-H clipper, which can clip many polygons at once very quickly. At least on OSX and Unix, this C code is auto-compiled (and maybe Windows, but I've never tried). If compilation fails, it falls back on the slow IDL version.

Web Coyote's Guide to IDL Programming