

Title  Determine whether a polygon is convex in Visual Basic .NET 
Description  This example shows how to determine whether a polygon is convex in Visual Basic .NET. 
Keywords  polygon, convex, concave, graphics, VB.NET 
Categories  Graphics, VB.NET 


Calculate the angles at each of the polygon's corners. If all of the angles have the same sign (either positive or negative depending on the orientation), then the polygon is convex. In the figure, for example, the angles have opposite signs (one is drawn clockwise and the other counterclockwise) so this polygon is not convex.


' Return True if the polygon is convex.
Public Function PolygonIsConvex(ByVal points() As PointF) _
As Boolean
' For each set of three adjacent points A, B, C,
' find the dot product AB dot BC. If the sign of
' all the dot products is the same, the angles
' are all positive or negative (depending on the
' order in which we visit them) so the polygon
' is convex.
Dim got_negative As Boolean = False
Dim got_positive As Boolean = False
Dim max_point As Integer = points.Length  1
Dim B, C As Integer
For A As Integer = 0 To max_point
B = A + 1
If B > max_point Then B = 0
C = B + 1
If C > max_point Then C = 0
Dim cross_product As Single = _
CrossProductLength( _
points(A).X, points(A).Y, _
points(B).X, points(B).Y, _
points(C).X, points(C).Y)
If cross_product < 0 Then
got_negative = True
ElseIf cross_product > 0 Then
got_positive = True
End If
If got_negative And got_positive Then Return False
Next A
' If we got this far, the polygon is convex.
Return True
End Function


For more information on graphics programming in Visual Basic, see my book Visual Basic Graphics Programming.





