Click to See Complete Forum and Search --> : Calculating area of a polygon by coordinates!
twotwobee
Jun 19th, 2006, 10:48 PM
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...........
twanvl
Jun 20th, 2006, 05:20 AM
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
twotwobee
Jun 20th, 2006, 10:50 AM
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.?
twanvl
Jun 20th, 2006, 10:55 AM
Sure, the algorithm I gave can be used like:
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.
twotwobee
Jun 20th, 2006, 12:17 PM
What does:
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:
Dim arrayX() as arraylist
Dim arrayY() as arraylist
Dim i as integer
area = 0
For i = 0 to numpoints - 1
area += arrayX(i) * arrayY(i + 1)............ (???)
next
twanvl
Jun 20th, 2006, 12:20 PM
By that for loop I meant what in VB you would write as:
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.
xxarmoxx
Apr 10th, 2007, 12:43 PM
This works like a charm for me.
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
obicd
Jul 22nd, 2011, 09:23 AM
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
krtxmrtz
Aug 3rd, 2011, 10:02 AM
This is what I use:
Type pnt
x As Single
y As Single
End Type
Function Area(p As pnt) As Single
Dim i As Integer, m As Integer
Dim a As Single
'Calculates the area of a closed contour (polygon) of any shape defined
'by an array of points p
'The last point closes the contour, i.e. the first and last points are the same:
'p(0) = p(m)
'where m is one less than the number of points:
m = UBound(p)
'Minimum number of points: m = 3 (i.e. a triangle)
If m < 3 Then
Area = 0
Exit Function
End If
a = 0
For i = 0 To m - 1
a = a + p(i).x * p.y(i + 1) - p.y(i) * p.x(i + 1)
Next
'The resulting a may be negative (depending whether the points are oriented CW or CCW)
'so the absolute value must be taken
Area = 0.5 * Abs(a)
End Function
leprekhaun
Sep 22nd, 2011, 04:06 AM
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.
krtxmrtz
Sep 22nd, 2011, 04:47 AM
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.
Function InOrOut(p, q, k, p0, q0) As Integer
'Function to determine the position of point (p0,q0) relative
'to the closed contour defined by the n points with
'coordinates p(i), q(i), i=1, 2, ..., k
'
'Point 1 must be the same as point k
'Returned function values():
' 1: point is inside the contour
' -1: point is outside the contour
' 0: point lies on the contour border
'*********************************************************************
Dim kross As Integer, i As Integer
Dim pp As Single
'Function initialization
InOrOut = 0
'Initialization of kross, a variable which keeps track
'of how many times the horizontal semi-infinite straight
'line starting at (p0,q0) and in the positive (right hand side)
'x axis direction intercepts the contour
'(The contour may have vertices with angles larger than 180 degrees)
kross = 0
'Loop over all contour sides
For i = 1 To k - 1
'If the side between the i and i+1 vertices lies entirely above
'or below the point (p0,q0), skip it (there is no intercept)
If (q(i) > q0 And q(i + 1) > q0) Or (q(i) < q0 And q(i + 1) < q0) Then GoTo NextItem
'If the side is horizontal avoid the calculation of the intercept by interpolation
'as there would be a division by zero
If q(i) = q(i + 1) Then
'It has to be determined if the point (p0,q0) lies on this horizontal segment
If (p(i) > p0 And p(i + 1) > p0) Or (p(i) < p0 And p(i + 1) < p0) Then GoTo NextItem
'If it doesn't, we're done!
Exit Function
End If
'Calculation of pp, the coordinate of the point where the segment connecting the
'sides i and i+1 and the horizontal straight line that goes through (p0,q0) intercept
pp = p(i) + (q0 - q(i)) * ((p(i + 1) - p(i)) / (q(i + 1) - q(i)))
'The sign of pp-p0 determines the position of the intercept relative to (p0,q0)
If pp - p0 > 0 Then
'Intercept to the right: increment counter (but only if the intercept does not lie
'on the first vertex, to avoid counting the same intercept twice!)
If q0 <> q(i) Then kross = kross + 1
ElseIf pp - p0 = 0 Then
Exit Function
End If
'If the intercept lies to the left, take no action and continue on
NextItem:
Next
'If the number of intercepts is even, the point (p0,q0) lies out of the contour
'If it's odd then it lies inside
If kross Mod 2 = 0 Then
InOrOut = -1
Else
InOrOut = 1
End If
End Function
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.