Convex Polygons

11 Oct 2023


§ Statement

Determining if PE\mathcal{P} \subset \mathbb{E} is a Convex Polygon

Suppose we are given the points P={Pi}i=0n1E2\mathcal{P} = \{P_i\}_{i=0}^{n-1} \subset \mathbb{E}^2 when connected in the given order yields a closed polygon Pc:P0P1Pn1P0.P_c : P_0 P_1 \dots P_{n-1} P_0.

How can we determine whether PcP_c is closed, if so, it is convex?

§ Solution

A necessary and sufficient condition for whether PcP_c is a polygon is for the interior angles summing to 180(n2)180(n - 2)^\circ, where nn is the number of edges; all polygons, convex or concave, have this property.

Next, if PcP_c 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)=12det(Pi,1,Pi+1,1,Pi+2,1),\begin{align*} \mathrm{area}(\triangle P_iP_{i+1}P_{i+2}) &= \frac{1}{2}\det (\langle P_i, 1\rangle, \langle P_{i+1}, 1\rangle, \langle P_{i+2}, 1\rangle), \end{align*}

if the sign of the area does not match for all iZni \in \mathbb{Z}_n, then we reject PcP_c 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+2PP_i, P_{i+1}, P_{i+2} \in \mathcal{P} and iZni \in \mathbb{Z}_n.

To do this, we define two vectors A=Pi+1PiA = P_{i + 1} - P_i and B=Pi+2Pi+1B = P_{i + 2} - P_{i + 1}, then the angle θi\theta_i can be found by

θi=arccos[ABAB]\begin{align*} \theta_i &= \arccos \left[\frac{A \cdot B}{\|A\|\|B\|}\right] \end{align*}

Therefore, if the sum of interior angles satisfies

i=0n1θi=180(n2),\begin{align*} \sum_{i=0}^{n-1} \theta_i = 180(n - 2), \end{align*}

then we accept PcP_c and say that it is a convex polygon; otherwise, we reject PcP_c.

§ Algorithm

Detecting a Convex Polygon

  • # Input: Set of points P={Pi}i=0n1\mathcal{P} = \{P_i\}_{i=0}^{n-1} with ordering PcS(P)P_c \in \mathfrak{S}(\mathcal{P})
  • # Output: Determination of polygon is CONVEX, NOT_CONVEX, or INVALID
  • # Note: NOT_CONVEX \neq 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\mathcal{P} is CONCAVE
  •  
  • function DetectConvex(P,Pc\mathcal{P}, P_c)
    • if n2n \leq 2 then

      • return INVALID
    •  

    • θ\theta \leftarrow 00

    • for all ii in Zn\mathbb{Z}_n do

      • Get vertices Pi,Pi1,Pi2P_i, P_{i-1}, P_{i-2} from PcP_c
      • sign \leftarrow sgn(area(PiPi+1Pi+2))\mathrm{sgn}(\mathrm{area}(\triangle P_{i}P_{i+1}P_{i+2}))
      •  
      • if direction is undefined then
        • direction \leftarrow sign
      •  
      • if sign <> direction then
        • return NOT_CONVEX
      •  
      • e1e_1 \leftarrow Pi+1Pi\overline{P_{i+1}P_i}
      • e2e_2 \leftarrow Pi+2Pi+1\overline{P_{i+2}P_{i+1}}
      • θ\theta \leftarrow θ+angle(e1,e2)\theta + \mathrm{angle}(e_1, e_2)
    •  

    • if θ\theta <> 180(n2)180(n - 2) then

      • return INVALID
    •  

    • return CONVEX

Complexity Analysis

This method has O(n)\mathcal{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\mathcal{P}.