BLOG.CSHARPHELPER.COM: Determine whether a polygon is convex in C#
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;
Comments