Convex Polygon - Problem
Convex Polygon Detection
Given an array of points on the X-Y plane
A polygon is convex if all interior angles are less than 180 degrees (π radians). Equivalently, when traversing the boundary, you should always turn in the same direction (either always left or always right).
Key Properties:
• The polygon is formed by connecting consecutive points
• The polygon is guaranteed to be simple (no self-intersections)
• Exactly two edges meet at each vertex
Goal: Return
Example: Points forming a square
Given an array of points on the X-Y plane
points where points[i] = [xi, yi], determine if these points form a convex polygon when connected in the given order.A polygon is convex if all interior angles are less than 180 degrees (π radians). Equivalently, when traversing the boundary, you should always turn in the same direction (either always left or always right).
Key Properties:
• The polygon is formed by connecting consecutive points
• The polygon is guaranteed to be simple (no self-intersections)
• Exactly two edges meet at each vertex
Goal: Return
true if the polygon is convex, false otherwise.Example: Points forming a square
[[0,0], [1,0], [1,1], [0,1]] create a convex polygon, while an L-shape would not be convex. Input & Output
example_1.py — Convex Square
$
Input:
points = [[0,0],[1,0],[1,1],[0,1]]
›
Output:
true
💡 Note:
The points form a square. All turns are 90-degree right turns, making it convex.
example_2.py — Non-convex L-shape
$
Input:
points = [[0,0],[0,10],[10,10],[10,5],[5,5],[5,0]]
›
Output:
false
💡 Note:
This forms an L-shape with both left and right turns, making it non-convex (concave).
example_3.py — Triangle
$
Input:
points = [[0,0],[1,1],[0,2]]
›
Output:
true
💡 Note:
Any triangle is always convex as all interior angles are less than 180 degrees.
Constraints
- 3 ≤ points.length ≤ 104
- points[i].length == 2
- -104 ≤ xi, yi ≤ 104
- All the given points are unique
- The polygon formed by the points is always a simple polygon
Visualization
Tap to expand
Understanding the Visualization
1
Form vectors
Create two consecutive edge vectors at each vertex
2
Calculate cross product
Cross product sign indicates left (+) or right (-) turn
3
Track consistency
All cross products must have the same sign for convexity
4
Handle collinear
Zero cross products (collinear points) are allowed and ignored
Key Takeaway
🎯 Key Insight: The cross product of consecutive edge vectors reveals turn direction. A polygon is convex if and only if all turns are in the same direction (all cross products have the same sign).
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code