Math question: How to compute the area of polygons

Tomas Nally, P.E. tnally at
Sun Mar 23 18:51:02 EST 2003


Subject: Math question: How to compute the area of polygons
>> Date: Tue, 18 Mar 2003 10:06:57 +0100
 Subject: Math question: How to compute the area of polygons
> From: malte.brill at <mailto:malte.brill at <mailto:malte.brill at> >  (Malte Brill)
> Hi List,
> this one goes out to the math experts.
> How does one compute the area ( I hope it is the correct Term for
> flaecheninhalt in german) of a polygon with n sides.

(Oops.  I'm late to this discussion because I had problems submitting a plain text email to the list.  Hope this one doesn't html-ize itself...)

Not a math "expert" here, but finding the areas of irregular polygons
was the one of the first things that I did with my very own computer
(a Commodore-64) when I bought it in 1985.  (Actually, I wasn't finding
the area of polygons, per se.  I was performing numerical integration
to see how close the result would be to the answer when traditional
calculus is used.  But that operation is almost identical to finding
the area of a polygon.)

I'm not too much of a Transcripter, so let me give a BASIC version,
from which a transcript version could be generated.

-- First, make sure that the x- and y-coordinates of the vertices of the polygon
-- are stored in an array.  Also, the array members should identify
-- the points of the polygon as you trace the polygon CLOCKWISE
-- around it's perimeter.  In BASIC, this is how arrays are
-- dimensioned.

dim x(100)
dim y(100)

-- Additionally, each differential element of the polygon will have
-- its own differential area, so we need an array for that also.

dim trapArea(100)

-- Identify the number of nodes of the polygon.  (Or, you might
-- just as well call them "vertices", or "points".)

n = 20  -- Just an example, here.

-- Now, going clockwise around the perimeter of the polygon,
-- find the area of the virtual trapezoid under each line segment.
-- A line segment is defined as the segment connecting
-- the ith node and the (i + 1)th node.  Do this inside a
-- counted loop.  In BASIC, a convenient loop for this is
-- the For...Next loop.

for i = 1 to (n - 1) -- note that I'm stopping at (n - 1)
   x1 = x(i)
   y1 = y(i)
   x2 = x(i + 1)
   y2 = y(i + 1)
   trapArea(i) = y1 * (x2 - x1) + (1/2) * (x2 - x1) * (y2 - y1)
next i

-- This doesn't quite "close the loop" around the perimeter
-- of the polygon.  In order to close the loop, we need to
-- find the area under the line segment between the nth
-- node and the very first node.

x1 = x(n)
y1 = y(n)
x2 = x(1)
y2 = y(1)
trapArea(n) = y1 * (x2 - x1) + (1/2) * (x2 - x1) * (y2 - y1)

-- Now, we have to add up all the areas of the elements

TotalArea = 0
for i = 1 to n
   TotalArea = TotalArea + trapArea(i)
next i

-- When I've discussed this elsewhere, there's usually
-- some skepticism about whether it works.  The reason
-- it works is because when the (i + 1)th node is on the
-- right-hand side of the ith node, then trapArea(i) has
-- a positive value.  However, as you traverse around
-- the perimeter of the polygon, when the (i + 1)th node
-- is on the left-hand side of the ith node, then the
-- value of trapArea(i) is negative.  When you add all
-- of these positive and negative areas, then the result
-- is the area on the interior of the polygon.
-- To test it, create a circle with points every 15 degrees
-- or so.  Then find the area using the code above.  It'll
-- be pretty close to the area found by A = Pi*R^2.  Of
-- course, the more points you use around the perimeter
-- of the circle, the more the area will converge on the
-- "exact" value.
-- What happens if the order of your points traces the
-- perimeter of the polygon in a COUNTERCLOCKWISE
-- (or anticlockwise) direction?  The area computed will
-- simply have a negative value.  So, it will be correct
-- in magnitude, but opposite in sign.

I hope the code above works correctly.  Sometimes
when I write it "on the fly", I miss something.  But
the principle is correct.  Back in November or 
December, I wrote a series of posts about this same
topic in the Liberty BASIC discussion group on
Yahoo.  The group seemed unconvinced until I produced

the demo that calculated the area of a circle to within

a few thousandths of a percent of the "exact" area.

---Nally, New Orleans

More information about the use-livecode mailing list