Math question: How to compute the area of polygons
Raymond E. Griffith
rgriffit at ctc.net
Mon Mar 24 04:36:36 EST 2003
on 3/21/03 9:46 PM, Jim Hurley at jhurley at infostations.com wrote:
>> 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,
>
Not quite: you are repeating the first line, but that does not mean that you
are closing the loop.
Now if you have a filled polygon, the first line and the last line in the
points are the same, so you have no problem. OTOH, if you don't have a
filled polygon or are simply submitting a set of points, you should do this:
put line -1 of tList into pOld
This will close the loop, and it is unlikely (though possible) that the
contribution will be equal to 0.
But the loop *must* be closed. If it is not closed you will likely get the
wrong area.
Cheers!
Raymond
More information about the use-livecode
mailing list