Math question: How to compute the area of polygons

Raymond E. Griffith tiffirgrReverse at ctc.net
Fri Mar 21 04:07:02 EST 2003


on 3/20/03 12:54 PM, Ben Rubinstein at benr_mc at cogapp.com wrote:

> on 20/3/03 4:24 pm, Malte Brill wrote
> 
>> Even though I start thinking I´m not smart enough to code a solution on my
>> own
>> at the moment
>> ...snip...
>> I'm still very interested in computing areas of polygons and their unions
> 
> I know this is no use for intersection testing, but for other possible - non
> time critical - requirements to calculate areas of polygons, Mr Crude
> Approach just can't help mentioning an alternative to the fascinating
> algorithms discussed, namely: let Scott do the hard work.
> 
> That is, get Rev to draw the polygon (or polygons), solid colour, against a
> different solid colour background.  Import a snapshot.  Read the image data.
> Count pixels of the appropriate colour.  Result: area of polygon or union of
> polygons. No maths training needed!  (Except addition.)  To calculate area
> of intersection, use two colours and an ink effect!
> 
> There.  I've said it.  Now I feel dirty and ashamed and will crawl off and
> hide.
> 
> Ben Rubinstein               |  Email: benr_mc at cogapp.com
> Cognitive Applications Ltd   |  Phone: +44 (0)1273-821600
> http://www.cogapp.com        |  Fax  : +44 (0)1273-728866
> 

Here is a little routine I have written that will accurately calculate the
area of irregular polygons so long as there are no crossed lines. You can
have irregular shapes as funky as you like and this will work -- provided
there are no lines which cross.

The math behind this is fascinating.

Area returned is in pixels. If you wish to use units as square cm, square
inches, etc. it can be appropriately adjusted.

function IrPolyArea i
  put the points of graphic i into k
  put 0,0 into n
  repeat for each line i in k
    add item 1 of i*item 2 of n-item 2 of i *item 1 of n to tsum
    put i into n
  end repeat
  return abs(tsum/2)
end IrPolyArea

Raymond E. Griffith




More information about the use-livecode mailing list