[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Polygon Area Calculation?



Thanks Ron, that suites my needs perfectly! Much appreciated.

And thanks to everyone else for their ideas and suggestions, as well.

mark

On Mon, Sep 29, 2008 at 2:32 PM, Ron Dippold <sizer@xxxxxxxxxx> wrote:
Not built in, but here's something I wrote a while ago based on calculating the consecutive trapezoids along the axis - this is a common algorithm. Note the caveats in the method docs.

  - Ron


def poly_area( points ):
       """
       Returns the area of any polygon IF the edges don't cross and
       the points are given in order around the edge.
       
       points are [ (x,y), (x,y) ... ]
       """
       
       area = 0
       # start with the last point so we close it (won't hurt if it already is)
       pt1 = points[-1]
       for pt2 in points:
               # we save the /2.0 for the return
               area += ( pt2[0] - pt1[0] ) * ( pt2[1] + pt1[1] ) # /2.0
               pt1 = pt2

       # counterclockwise poly returns negative area
       if area<0:
               area *= -1

       return area/2.0



Mark S wrote:
Hi all,

I'm wondering if there is anything built in to pygame that will report the area of a polygon?