Math question: How to compute the area of polygons

Jim Hurley jhurley at infostations.com
Fri Mar 21 21:49:00 EST 2003


>Jim Hurley wrote:
>on mouseUp
>     put field 1 into tList
>     put 0 into area
>     put  line 1 of tList into pOld
>     repeat with i = 1 to the number of lines in tList
>       put  line i of tList into  pNew
>       add thisToArea(pNew,pOld) to area
>       put pNew into pOld
>     end repeat
>     put area
>end mouseUp
>
>function thisToArea ptNew,ptOld
>     put item 1 of ptNew into xNew
>     put item 2 of ptNew into yNew
>     put item 1 of ptOld into xOld
>     put item 2 of ptOld into yOld
>     return -(xNew - xOld)*(yNew + yOld)/2
>end thisToArea
>
>
>  >    put  line 1 of tList into pOld
>

Dar Scott wrote:

>Should this be -1 to get the last edge?
>


Dar,

Actually it contributes nothing to the area since the  first term in 
the sum is the area under the line joining the first point with 
itself; not very smart but, no harm, no foul.  It would  have been 
more sensible if I had started the sum with i = 2.

Since the last point in the list is identical to the first point (it 
is a *closed* polygon), the last edge is included by performing the 
repeat up to the number of lines (points in the polygon) in tList.

But a cleaner (and faster) version might be:

on mouseUp
     put field 1 into tList
     put 0 into area
     put  line 1 of tList into pOld
     repeat for each line tLine in tList
       add thisToArea(tLine,pOld) to tArea
       put tLine into pOld
     end repeat
     put tArea
end mouseUp

But you still get no contribution from the first repeat, but what is 
0 among friends.

Regards,

Jim



More information about the use-livecode mailing list