|
|
Title | Calculate a polygon's area |
Keywords | polygon, area |
Categories | Graphics |
|
|
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).
|
|
Private Function SignedPolygonArea() As Single
Dim pt As Integer
Dim area As Single
' Add the first point to the end.
ReDim Preserve m_Points(1 To m_NumPoints + 1)
m_Points(m_NumPoints + 1) = m_Points(1)
' Get the areas.
For pt = 1 To m_NumPoints
area = area + _
(m_Points(pt + 1).X - m_Points(pt).X) * _
(m_Points(pt + 1).Y + m_Points(pt).Y) / 2
Next pt
' Return the result.
SignedPolygonArea = area
End Function
' Find the polygon's area.
Public Function PolygonArea() As Single
' Return the absolute value of the signed area.
PolygonArea = Abs(SignedPolygonArea())
End Function
|
|
For more information on graphics programming in Visual Basic, see my book Visual Basic Graphics Programming.
|
|
|
|
|
|