Results 1 to 4 of 4

Thread: VB Snippet - Math - Algorithm for the area of any polygon

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    VB Snippet - Math - Algorithm for the area of any polygon

    VB Code:
    1. Public Function Area(xpoly, ypoly, npoly)
    2.  
    3. '***********************************************************************
    4. 'Calculates the area of a closed contour (polygon) of any shape
    5. 'defined by xpoly(i),ypoly(i) with i=1,2,...,npoly
    6. 'The last point must close the contour, i.e.
    7. 'xpoly(1)=xpoly(npoly) & ypoly(1)=ypoly(npoly)
    8. 'Minimum npoly is 4 (i.e. a triangle)
    9. '***********************************************************************
    10.     Dim a As Single
    11.    
    12.     'Check for minimum num. of pts.
    13.     If npoly < 4 Then
    14.         Area = 0
    15.         Exit Function
    16.     End If
    17.    
    18.     a = 0   'Initialize area
    19.     For i = 1 To npoly - 1
    20.         a = a + xpoly(i) * (ypoly(i + 1) - ypoly(i)) - ypoly(i) * (xpoly(i + 1) - xpoly(i))
    21.     Next
    22.    
    23.     Area = 0.5 * a
    24. End Function
    Last edited by krtxmrtz; May 7th, 2003 at 08:49 AM.

  2. #2

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Warning: error spotted and corrected

    I have found that this function I posted some time ago in the VB code bank for area calculations was wrong and returned incorrect results (in some cases).
    I thought it was working fine when I first checked it because the inaccuracies were small and thus difficult to spot for polygons with a large number of vertices, but then I found it failed in the most trivial cases.

    I have re-edited the post and rewritten the code. Now the function is much simpler (and hopefully correct). I apologize for any inconveniences.

  3. #3

  4. #4

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Ok thanks, now it makes more sense. Just wanted to make sure it didn't go unnoticed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width