Home Search   What's New Index Books Links Q & A Newsletter Banners   Feedback Tip Jar MSDN Visual Basic Community

Title Calculate a polygon's area in Visual Basic .NET This example shows how to calculate a polygon's area in Visual Basic .NET. polygon, area, graphics, VB.NET Graphics, VB.NET

Add the areas of the trapezoids defined by the polygon's edges dropped to the X-axis. When the program considers a bottom edge of a polygon, the calculation gives a negative area so the space between the polygon and the axis is subtracted, leaving the polygon's area. This method gives odd results for non-simple polygons (where edges cross).

The total calculated area is negative if the polygon is oriented clockwise. See also the HowTo Find the orientation of a polygon (clockwise or counterclockwise) in Visual Basic .NET.

```Public Function PolygonArea(ByVal points() As PointF) As _
Single
' Return the absolute value of the signed area.
' The signed area is negative if the polyogn is
' oriented clockwise.
Return Math.Abs(SignedPolygonArea(points))
End Function
<P>
Private Function SignedPolygonArea(ByVal points() As _
PointF) As Single
' Add the first point to the end.
ReDim Preserve points(points.Length)
points(points.Length - 1) = points(0)

' Get the areas.
Dim area As Single = 0
For i As Integer = 0 To points.Length - 2
area += _
(points(i + 1).X - points(i).X) * _
(points(i + 1).Y + points(i).Y) / 2
Next i

' Return the result.
Return area
End Function```