Results 1 to 11 of 11

Thread: Calculating area of a polygon by coordinates!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    80

    Calculating area of a polygon by coordinates!

    I have an array with a set of points (X Y coordinates), and the number of points in a polygon. How do I calculate the surface area of this closed polygon?

    (I have no height value for this polygon... just a set of XY coordinates and NumOfPoints).

    Thanks...........

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: Calculating area of a polygon by coordinates!

    The total area of a polygon is the sum of the area of the triangles p1p2p3, p1p3p4, p1pn-1pn. This gets you a kind of fan shape. Obviously this can include areas outside the polygon if the polygon is not convex. If you instead subtract the area when the three points of a triangle are in clockwise order, you will subtract the areas outside the polygon again.

    For calculating the area of a triangle (and also finding out if it is clockwise or not) you can use a cross product. A123 = |(p2-p1) X (p3-p1)| / 2

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    80

    Re: Calculating area of a polygon by coordinates!

    I only have something like

    x1 y1
    x2 y2
    x3 y3
    x4 y4
    x5 y5
    x6 y6

    I'm trying to do this in vb.net code with a loop. Is there a formula out there where I can use just these points, and the # of pts (6 in the case above)? Without having to worry about direction, triangles, etc.?

  4. #4
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: Calculating area of a polygon by coordinates!

    Sure, the algorithm I gave can be used like:
    Code:
    area := 0
    for i from 3 to n
        v1x := x[i] - x[1]
        v1y := y[i] - y[1]
        v2x := x[i] - x[2]
        v2y := y[i] - y[2]
        area := area + v1x*v2y - v2x*v1y
    // we may get a negative area
    area := abs(area)
    Note that I haven't tested this algorithm, so it may be wrong.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Posts
    80

    Re: Calculating area of a polygon by coordinates!

    What does:

    VB Code:
    1. for i from 3 to n

    mean?

    If I have like 10 points (10 x values and 10 y values), would that algorithm be a little tedious?

    I'm thinking something along like:

    VB Code:
    1. Dim arrayX() as arraylist
    2. Dim arrayY() as arraylist
    3. Dim i as integer
    4.  
    5. area = 0
    6.  
    7. For i = 0 to numpoints - 1
    8.     area += arrayX(i) * arrayY(i + 1)............  (???)
    9. next

  6. #6
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: Calculating area of a polygon by coordinates!

    By that for loop I meant what in VB you would write as:
    Code:
    for i = 2 to number_of_points
    And by x[i] I mean the ith element of an array (where 1 is the first index), so arrayX(i) in your VB code. If the arrays start at 0, then you just change the loop to go from 0 to numpoints-1, and use x(0) instead of x(1), y(1) instead of y(2), etc.

  7. #7
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: Calculating area of a polygon by coordinates!

    This works like a charm for me.



    Code:
    Public Function getArea(ByVal a As ListBox, ByVal b As ListBox)
    
             ' Make sure to add the first element to the end to close the polygon
            a.Items.Add(a.Items(0))
            b.Items.Add(b.Items(0))
    
            Dim n, area, i
            n = a.Items.Count
    
            i = 0
            area = 0
    
            Do
                area += (a.Items.Item(i) * b.Items.Item(i + 1) - a.Items.Item(i + 1) * b.Items.Item(i))
    
                i = i + 1
            Loop While i < n - 1
    
            area = Math.Abs(area * 0.5)
    
            ' if you have a certain scale you can use this, or skip it
            area = applyScale((9 / 64), area)
    
            Return area
        End Function
    Last edited by xxarmoxx; Apr 11th, 2007 at 04:16 PM.

  8. #8
    New Member
    Join Date
    Jul 2011
    Posts
    2

    Re: Calculating area of a polygon by coordinates!

    Quote Originally Posted by twotwobee View Post
    I have an array with a set of points (X Y coordinates), and the number of points in a polygon. How do I calculate the surface area of this closed polygon?

    (I have no height value for this polygon... just a set of XY coordinates and NumOfPoints).

    Thanks...........
    I have got a simple Surveying Software -SurveyBuddy - that has an area compuation application, which could compute the area from any number of coordinates.

    Email address removed by mod

  9. #9
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Calculating area of a polygon by coordinates!

    This is what I use:
    VB Code:
    1. Type pnt
    2.     x As Single
    3.     y As Single
    4. End Type
    5. Function Area(p As pnt) As Single
    6.     Dim i As Integer, m As Integer
    7.     Dim a As Single
    8.  
    9.     'Calculates the area of a closed contour (polygon) of any shape defined
    10.     'by an array of points p
    11.    
    12.     'The last point closes the contour, i.e. the first and last points are the same:
    13.    
    14.     'p(0) = p(m)
    15.    
    16.     'where m is one less than the number of points:
    17.     m = UBound(p)
    18.    
    19.     'Minimum number of points: m = 3 (i.e. a triangle)
    20.     If m < 3 Then
    21.         Area = 0
    22.         Exit Function
    23.     End If
    24.    
    25.     a = 0
    26.     For i = 0 To m - 1
    27.         a = a + p(i).x * p.y(i + 1) - p.y(i) * p.x(i + 1)
    28.     Next
    29.    
    30.     'The resulting a may be negative (depending whether the points are oriented CW or CCW)
    31.     'so the absolute value must be taken
    32.     Area = 0.5 * Abs(a)
    33.    
    34. End Function
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  10. #10
    New Member
    Join Date
    Sep 2011
    Posts
    1

    Re: Calculating area of a polygon by coordinates!

    Hi,
    sorry to dig up such an old thread, but I am getting desperate now and this seems kind of close.
    I am trying to calculate if a point is within a polygon. I have a series of polygons all mapped out using latitude and longitude points and I have a series of markers with lat and long refernces for them and I am trying to work out if they fall within the polygon or not. Ideally it would be great to say which one it does fall in but I dont want to over complicate things.

    Thanks for any help.

  11. #11
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Calculating area of a polygon by coordinates!

    Quote Originally Posted by leprekhaun View Post
    Hi,
    sorry to dig up such an old thread, but I am getting desperate now and this seems kind of close.
    I am trying to calculate if a point is within a polygon. I have a series of polygons all mapped out using latitude and longitude points and I have a series of markers with lat and long refernces for them and I am trying to work out if they fall within the polygon or not. Ideally it would be great to say which one it does fall in but I dont want to over complicate things.

    Thanks for any help.
    I wrote this function long time ago and haven't used it in ages so, while I expect it to work I'm not 100% sure. Just try it out. I think the comments should be explicit enough about the way it works.
    Also, I always work with 0-based arrays but at that time I had not yet adopted this policy so they are 1-based. Not that it matters much in this case, though.
    VB Code:
    1. Function InOrOut(p, q, k, p0, q0) As Integer
    2.  
    3. 'Function to determine the position of point (p0,q0) relative
    4. 'to the closed contour defined by the n points with
    5. 'coordinates p(i), q(i), i=1, 2, ..., k
    6. '
    7. 'Point 1 must be the same as point k
    8. 'Returned function values():
    9. '       1: point is inside the contour
    10. '      -1: point is outside the contour
    11. '       0: point lies on the contour border
    12. '*********************************************************************
    13.     Dim kross As Integer, i As Integer
    14.     Dim pp As Single
    15.    
    16.     'Function initialization
    17.     InOrOut = 0
    18.     'Initialization of kross, a variable which keeps track
    19.     'of how many times the horizontal semi-infinite straight
    20.     'line starting at (p0,q0) and in the positive (right hand side)
    21.     'x axis direction intercepts the contour
    22.     '(The contour may have vertices with angles larger than 180 degrees)
    23.     kross = 0
    24.     'Loop over all contour sides
    25.     For i = 1 To k - 1
    26.         'If the side between the i and i+1 vertices lies entirely above
    27.         'or below the point (p0,q0), skip it (there is no intercept)
    28.         If (q(i) > q0 And q(i + 1) > q0) Or (q(i) < q0 And q(i + 1) < q0) Then GoTo NextItem
    29.         'If the side is horizontal avoid the calculation of the intercept by interpolation
    30.         'as there would be a division by zero
    31.         If q(i) = q(i + 1) Then
    32.             'It has to be determined if the point (p0,q0) lies on this horizontal segment
    33.             If (p(i) > p0 And p(i + 1) > p0) Or (p(i) < p0 And p(i + 1) < p0) Then GoTo NextItem
    34.             'If it doesn't, we're done!
    35.             Exit Function
    36.         End If
    37.         'Calculation of pp, the coordinate of the point where the segment connecting the
    38.         'sides i and i+1 and the horizontal straight line that goes through (p0,q0) intercept
    39.         pp = p(i) + (q0 - q(i)) * ((p(i + 1) - p(i)) / (q(i + 1) - q(i)))
    40.         'The sign of pp-p0 determines the position of the intercept relative to (p0,q0)
    41.         If pp - p0 > 0 Then
    42.             'Intercept to the right: increment counter (but only if the intercept does not lie
    43.             'on the first vertex, to avoid counting the same intercept twice!)
    44.             If q0 <> q(i) Then kross = kross + 1
    45.         ElseIf pp - p0 = 0 Then
    46.             Exit Function
    47.         End If
    48.         'If the intercept lies to the left, take no action and continue on
    49. NextItem:
    50.     Next
    51.     'If the number of intercepts is even, the point (p0,q0) lies out of the contour
    52.     'If it's odd then it lies inside
    53.     If kross Mod 2 = 0 Then
    54.         InOrOut = -1
    55.     Else
    56.         InOrOut = 1
    57.     End If
    58.    
    59. End Function
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

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