Determine whether a polygon is convex in C#

To see if a polygon is convex, 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.

// Return True if the polygon is convex.
public bool PolygonIsConvex()
{
// For each set of three adjacent points A, B, C,
// find the dot product AB ยท 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.
bool got_negative = false;
bool got_positive = false;
int num_points = Points.Length;
int B, C;
for (int A = 0; A < num_points; A++)
{
B = (A + 1) % num_points;
C = (B + 1) % num_points;

float cross_product =
CrossProductLength(
Points[A].X, Points[A].Y,
Points[B].X, Points[B].Y,
Points[C].X, Points[C].Y);
if (cross_product < 0)
{
got_negative = true;
}
else if (cross_product > 0)
{
got_positive = true;
}
if (got_negative && got_positive) return false;
}

// If we got this far, the polygon is convex.
return true;
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.