What is the formula to rotate each of these polygons in order to stay "straight"?

I'm doing some programming, and the library I'm using generates the following n-gons: enter image description here

The way it does is that it generates the first vertex in the right at the same height as the center of the ngon, and then it generates the other vertices.

My question is what is the formula in order to obtain the angle that makes each polygon straight, for example, I want the triangle to be $\triangle$, and the square to be $\square$. For them, I can rotate the triangle $60$ degrees in one direction or $90$ degrees in the other. For the square I can rotate $45$ degrees. And so on.


Let $n$ be the number of sides or vertices of your polygon. If you call "bottom side" the side of the polygon with the lowest vertices, then you are looking for the smallest rotation that will make the bottom side horizontal. As if the polygon is resting on a table.

In the following, without loss of generality, we assume the polygon to be of radius $1$, centered on the origin of the plane.

Your library seems to generate the polygon via the following procedure: It generates $n$ points, for $k=0\dots n-1$ with coordinates $(x_k, y_k)$: $$\left\{\begin{split} x_k &= \cos\left(\frac {2\pi k}n\right)\\ y_k &= \sin\left(\frac {2\pi k}n\right) \end{split}\right. $$ The bottom side has the vertex with the lowest $y_k$ coordinate, so it is for index $k^*$ that satisfies $$k^*=\arg_k\min \left|\frac{2\pi k}{n}-\frac{3\pi}{2}\right|=\arg_k\min \left|k-\frac{3n}{4}\right|\tag{1}$$ The other vertex of the bottom slide is for any second best solution $k$ after $k^*$ to minimize the above, so it's either $k^*+1$ or $k^*-1$. Let's call it $k^{**}$. Those indices are to be taken modulo $n$. Alternatively, if you don't want to deal with modulos, note that as soon as $n\geq 5$, then $k^*$ verifies $0<k<n-1$, which ensures that $0\leq k^{**}\leq n-1$ (and you already know the solution is for the triangle and square).

enter image description here

The solution to $(1)$ is either $k^*=\lfloor\left(\frac{3n}4\right)\rfloor$ (floor function) or $k^*=\lceil\left(\frac{3n}4\right)\rceil$ (ceil function). When those two numbers are distinct, $k^*$ is one of them, and $k^{**}$ is the other one. When those two numbers are equal ($n$ is a multiple of $4$), then $k^*$ is this number, and $k^{**}$ is $k^*+1$ or $k^*-1$ (either will do).

Let's look at the angle between the bottom side and the horizontal direction. It's given by $$\begin{split} \theta_n &= \arctan\left( \frac{y_{k^{**}}-x_{y^{*}}}{x_{k^{**}}-x_{k^{*}}}\right)\\ & = \arctan\left( \frac{\sin\left(\frac{2\pi k^{**}}{n}\right)-\sin\left(\frac{2\pi k^{*}}{n}\right)}{\cos\left(\frac{2\pi k^{**}}{n}\right)-\cos\left(\frac{2\pi k^{*}}{n}\right)}\right)\\ &=-\arctan\left(\cot\left(\frac{\pi(k^*+k^{**}}{n}\right)\right) \end{split}$$ To straighten your polygon, you must rotate it around the center by $-\theta_n$.

In summary, the algorithm is (changing the notation a bit):

  • Define $p=\lfloor\left(\frac{3n}4\right)\rfloor$.

  • Compute $$\theta_n = \arctan\left( \cot\left(\frac{\pi(2p+1)}{n}\right)\right)=\frac{3\pi}2-\frac{\pi(2p+1)}{n}$$

  • Rotate by $\theta_n$ around the center of the polygon.

First few numerical values: $$\begin{array}{|c|c|} \hline n&3&4&5&6&7&8&9&10\\ \hline \theta_n &-\frac \pi 6&-\frac\pi 4&\frac \pi {10}&0&-\frac{\pi}{14}&-\frac{\pi}{8}&\frac{\pi}{18}&0\\ \hline \end{array}$$