Convex Polygons
11 Oct 2023
Statement
Determining if P⊂E is a Convex Polygon
Suppose we are given points P={Pi}i=0n−1⊂E2 that yields a closed polygon Pc:P0P1…Pn−1P0 when they are connected in the given order.
How can we determine whether Pc is closed, if so, it is convex?
Solution
A necessary and sufficient condition for whether Pc is a polygon is for the interior angles summing to 180(n−2)∘, where n is the number of edges; all polygons, convex or concave, have this property.
Next, if Pc convex, then every pair of edges must turn in the same direction.
The direction of turning can be found by using the determinant definition for the area of a triangle,
area(△PiPi+1Pi+2)=21det(⟨Pi,1⟩,⟨Pi+1,1⟩,⟨Pi+2,1⟩),
if the sign of the area does not match for all i∈Zn, then we reject Pc and say that it is not a convex polygon.
Next, we compute the interior angles of each pair of edges formed by points Pi,Pi+1,Pi+2∈P and i∈Zn.
To do this, we define two vectors A=Pi+1−Pi and B=Pi+2−Pi+1, then the angle θi can be found by
θi=arccos[∥A∥∥B∥A⋅B]
Therefore, if the sum of interior angles satisfies
i=0∑n−1θi=180(n−2),
then we accept Pc and say that it is a convex polygon; otherwise, we reject Pc.
Algorithm
Detecting a Convex Polygon
- # Input: Set of points P={Pi}i=0n−1 with ordering Pc∈S(P)
- # Output: Determination of polygon is
CONVEX, NOT_CONVEX, or INVALID
- # Note:
NOT_CONVEX = CONCAVE; it indicates a change in winding order
- # (i.e. anticlockwise to clockwise, or vice versa).
- # It is a trivial modification to check if P is
CONCAVE
-
- function DetectConvex(P,Pc)
-
if n≤2 then
-
-
θ ← 0
-
for all i in Zn do
- Get vertices Pi,Pi−1,Pi−2 from Pc
sign ← sgn(area(△PiPi+1Pi+2))
-
- if
direction is undefined then
direction ← sign
-
- if
sign <> direction then
-
- e1 ← Pi+1Pi
- e2 ← Pi+2Pi+1
- θ ← θ+angle(e1,e2)
-
-
if θ <> 180(n−2) then
-
-
return CONVEX
Complexity Analysis
This method has O(n) complexity, as we iterate through each point once per step and the computations in each iteration are all constant constant with respect to the input P.