Math question: How to compute the area of polygons

Heather Williams heather at runrev.com
Fri Mar 21 07:06:01 EST 2003


One more solution for you all to consider. Tomas had some posting troubles
so he asked me to post this for him.

  > From: Tomas Nally, P.E. on behalf of Tomas Nally, P.E.
    > Sent: Tue 3/18/2003 9:10 AM
    > To: use-revolution at lists.runrev.com
<mailto:use-revolution at lists.runrev.com>
    > Cc:
    > 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 t-online.de <mailto:malte.brill at t-online.de>
(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.
    >
    > 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 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.
    
-- 
Heather Williams <heather at runrev.com> <http://www.runrev.com/>
Runtime Revolution Ltd.
Tel: +44 (0) 131 7184333 Fax: +44 (0)1639 830707
Revolution: Software at the Speed of Thought




More information about the use-livecode mailing list