# Given sequence of angles, find a polygon

Find a polygon $p_{1}…p_{n}$, such that the angle at vertex $p_{i}$ is $α_{i}$.

Define $p_{n+1}=p_{1}$ for a polygon $p_{1}…p_{n}$. If one consider a polygon’s sides are vectors, then a polygon is set of vectors that equals to 1, such that the magnitude of the vectors are greater than 0.

Angles at each vertex determines the direction of the vector. Let $u_{i}$ be the unit vector with direction of $p_{i}p_{i+1}$, we have

$i=1∑n c_{i}u_{i}=0$

$c_{i}>0$ for all $i$. $u_{i}$ can be computed from $α_{i}$ trivially. We assume we have normalized the polygon by rotate it, so $u_{1}=(0,1)$. Once we find a set of $c_{i}$, then we determine a solution to the problem. $(c_{1},…,c_{n})$ is in the intersection of the solutions of two set of linear systems. One might first calculate the solution to the linear systems individually, then take the intersection, and pick a point where all coordinates are positive.

This is not easy to code, and it take $O(n_{3})$ time to just solve the linear system. A better and faster solution exploits the fact we are operating in a small dimension.

Observe that a polygon lies on a plane, a 2D space, we might think we can arbitrarily put down the length of $n−1$ vectors, and calculate the last one from it. It is not true, the $c_{i}>0$ condition shows ${c_{1}u_{1}+c_{2}u_{2}∣c_{1},c_{2}>0}$ does not contain the origin unless $u_{1}=−u_{2}$.

If we can find $u_{1},u_{2},u_{3}$, such that the convex hull of them contains the origin (here the convex hull means the open polygon contained inside), then clearly we can use $u_{1},u_{2},u_{3}$ to span the entire plane. The span here is defined as all the values can be obtained from conical combination. This inspires the following algorithm:

Find $u_{x},u_{y},u_{z}$ that spans the entire plane by checking if the convex hull contains $(0,0)$. Arbitrarily assign $c_{i}$, such that $i=y,z$. Calculate $c_{y}$ and $c_{z}$ from the assignment.

Once $u_{x},u_{y},u_{z}$ are found, only $O(n)$ time is required to find the polygon. Find the three spanning vectors is linear time. Pick any two adjacent vectors as $u_{x},u_{y}$, and do a search for $u_{z}$ that satisfy the condition. One can find a solution in $O(n)$ time.

If the algorithm fails, there are two possibilities:

- There is no such polygon.
- There are only four directions, and they are exactly $(1,0),(0,1),(−1,0),(0,−1)$. We can solve this special case by arbitrarily assign $c_{i}$ except the 2 of the directions. Calculate the length for those two from the other ones. This special case can be solved in $O(n)$ time too.