Convex Polygon
A polygon is convex if and only if the cross product of every three consecutive vertices has the same sign (either positive or negative). We can determine the sign of the cross product by evaluating the z-coordinate of the cross product of vectors formed by three consecutive vertices.
Here’s how you can implement the method:
- Create a helper function to calculate the cross product.
- Iterate through the polygon, taking three consecutive points at a time, and check the sign of the cross product.
- If at any point you find the cross products have different signs, return False. Otherwise, return True.
Here’s the code:
|
|
This code checks if the given polygon is convex by computing the cross product for three consecutive vertices and ensuring that the sign of the cross product is consistent across all vertices.