Polygon

|View all articles regarding Polygon|

Polygon is a python package that handles polygonal shapes in 2D. It contains Python bindings for gpc, the excellent General Polygon Clipping Library by Alan Murta and some extensions written in C and pure Python. With Polygon you may handle complex polygonal shapes in Python in a very intuitive way. Polygons are simple Python objects, clipping operations are bound to standard operators like +, -, |, & and ^. TriStrips can be constructed from Polygons with a single statement. Functions to compute the area, center point, convex hull, point containment and much more are included.

This package was already used to process shapes with more than one million points!

  • License: LGPL (not for gpc itself, see below!)
  • Status: Should be almost stable now, but there may be memory leaks. Malformed files and illegal contours may crash the library and your running Python interpreter! Use Polygon at your own risk or don't use it at all!
  • gpc-Homepage: http://www.cs.man.ac.uk/~toby/alan/software/

gpc is included in Polygon, you don't need to download it separately. I made two small changes to gpc:

  1. fixed warnings regarding a printf format string and
  2. made GPC_EPSILON adjustable, this may slow down the clipping a little bit.

The author of gpc (Alan Murta) is not responsible for this distribution! The wrapping and extension code is free software, but the core gpc library is free for non-commercial usage only. The author says:

GPC is free for non-commercial use only. We invite non-commercial users to make a voluntary donation towards the upkeep of GPC. If you wish to use GPC in support of a commercial product, you must obtain an official GPC Commercial Use Licence from The University of Manchester.

Please respect this statement and contact the author (see gpc homepage) if you wish to use this software in commercial projects!

Download, Documentation & Examples

Documentation and examples are included in the source distribution in the folder doc. The development of the packages is hosted at github.  There are two different projects, one for Python 2.x and one for Python versions 3.1 and newer.

Polygon 2.x for Python 2

Polygon 3.x for Python 3

 

10 Responses

  1. Simon 1. June 2011 at 00:11 |

    Tried that first – just putting a print “hello” before the if statement.

    Doesn’t work. I wonder if it’s because I’m using Python 2.5…

    Thanks for your help,

    Simon

  2. Simon 31. May 2011 at 13:44 |

    Hi Jörg,

    Thanks very much for the tip. I was indeed working int the install library which was somewhat lazy of me. It works fine elsewhere!

    I now have another issue – can you help me? I am looking to extend the code to provide grow and shrink operations for the polygons. I have already figured out the algorithm when I was using gpc with MATLAB – but now I want to use it with Python.

    I would like to be able to overload the ‘+’ and ‘-’ operators for this, so the following:

    x=y+2

    will make x a polygon that’s the y polygon grown by 2.

    There appears to be strong type checking in the cPolygon.c and this causes an error when I try to implement the above. At first I suspected that the Py_TPFLAGS_CHECKTYPES might be set – but it isn’t.

    I can’t find where the type checking is enabled – and so can’t disable it.

    Any ideas?

    Simon

  3. Simon 30. May 2011 at 14:22 |

    Hi Jörg,
    Thanks for those great bindings.
    Any idea what the following is all about?

    Python 2.5.1 (r251:54863, Aug 12 2010, 11:27:22)
    [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-59)] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import Polygon
    Traceback (most recent call last):
    File “”, line 1, in
    File “Polygon/__init__.py”, line 6, in
    from cPolygon import *
    ImportError: No module named cPolygon

  4. decitre 3. February 2011 at 14:53 |

    Hi Jörg,
    Thanks for those great bindings.
    Could you please change “”.join(l) into b”".join(l) in py3k version of encodeBinary() ?

  5. gaffg 1. January 2011 at 15:17 |

    thanks for the code. This is great

  6. Diederik van Lierop 30. November 2010 at 15:49 |

    Hi Jörg,

    Thanks for providing the Python bindings for the polygon library, that’s really much appreciated! There’s however a small glitch I ran into while exporting svg files: these files cannot be viewed in all SVG capable programs, e.g. Inkscape or Firefox. This issue has been reported here: https://bugs.launchpad.net/inkscape/+bug/683066. I’m not sure if this export functionality has been created by you or by Alan Murta, but I figured that reporting it here wouldn’t hurt.

    Thanks once again.

    Diederik van Lierop

Leave a Reply