I created these images way back in 2019 before I learned programming:

enter image description here

enter image description here

Now I am trying to programmatically generate images like them using Python.

Basically, I want to make something like this:

In short, you start off with a regular polygon with n sides, then you create new regular polygon of the same number of sides such that the vertices of the new polygon are on the sides of the original polygon, and each of the sides of the new polygon and the corresponding side in the original polygon form an angle of x degrees.

Then you create a new regular polygon with n sides whose vertices are on the second polygon and the pairs of corresponding sides also form an angle of x degrees. And then you recursively do this until a certain number of iterations is reached.

I want to know given the number of sides of the polygon, and the angle of rotation, how to calculate the corresponding distance the vertices need to be displaced along its sides.

For example, I have calculated the cases of squares and equilateral triangles:

enter image description here

For the square, let its side be s, the angle be a, it is easy to see the two legs of the right triangle formed by the rotated side and the original sides add together to s.

let x be the distance needed, let y be s - x.

It is easy to see y = x / tan(a)

x = s*tan(a)/(tan(a)+1)

So the vertices need to be displaced along the sides by s*tan(a)/(tan(a)+1)

enter image description here

For the equilateral triangle it is harder to calculate the distance needed, but if you draw a line from one of the rotated vertices perpendicular to its corresponding side in the original triangle, the right triangle formed by the operation will always contain a 30° angle.

Let x be the amount of displacement needed, and let y be s - x, x can be calculated with the following:

x + y = s
y = x/2 + x*cos(30°)/tan(a)
x + x/2 + x*cos(30°)/tan(a) = s
3x/2 + x*cos(30°)/tan(a) = s

Unfortunately my knowledge of geometry stops here, I can't calculate the amount of displacement of the vertices along the sides needed for regular polygons with more sides.

I know how to calculate the coordinates of vertices of regular polygon of given number of sides, I just don't know the amount of displacement corresponding to angles. Can anyone generalize the calculations I used?

Fixed logical reasoning errors in the question body, I was busy with other things so I didn't notice the error until now.

I did it!

enter image description here


  • $\begingroup$ @user21820 - Aren't questions with MathJax in the title not allowed on the HNQ list? This was on the list a few minutes ago. $\endgroup$
    – mr_e_man
    May 30, 2022 at 19:16
  • 1
    $\begingroup$ Isn't MathJax the norm here (the body)? $\endgroup$ May 30, 2022 at 19:16
  • $\begingroup$ @mr_e_man: As per Math SE guidelines, we should use descriptive titles using MathJax as appropriate. Whether MathJax is allowed on the HNQ or not should never be a reason to degrade the quality of question titles on Math SE. $\endgroup$
    – user21820
    May 30, 2022 at 19:20
  • $\begingroup$ @user21820 - Yes, but I don't think it's necessary for quality in this case. "tilted by a given angle" would be just as descriptive. $\endgroup$
    – mr_e_man
    May 30, 2022 at 19:21
  • $\begingroup$ @mr_e_man: Feel free to ask a moderator about this. $\endgroup$
    – user21820
    May 30, 2022 at 19:22

1 Answer 1


If one iterates over the sides of a regular polygon with side length 1, let $\lambda$ be the constant value such that one must go a distance of $\lambda$ along each side of the polygon to find the vertices of the polygon in the next iteration (going in an anti-clockwise direction).

We find using trigonometry this constant value of $\lambda$ for any given angle $\alpha$ and $n$ being the number of sides of the polygon we wish to iterate on. An interior angle $\beta$ of the polygon will be $180\cdot\frac{n-2}{n}$ degrees. By the law of $\sin$es, $\frac{\sin^2 \alpha}{\lambda^2} = \frac{\sin^2 \beta}{c^2}$, where $c^2 = 2\lambda^2 - 2\lambda + 1 + 2\lambda(\lambda-1)\cos\beta$ (by the law of cosines), and so therefore $\frac{c^2}{\lambda^2} = \frac{\sin^2\beta}{\sin^2\alpha}$.

This is a quadratic in $\mu = \frac{1}{\lambda}$, which can be written $\mu^2 - 2(1+\cos\beta)\mu + 2(1 + \cos\beta) = \frac{\sin^2\beta}{\sin^2\alpha}$. Solving for $\mu$, we get: $$\mu = 1+\cos\beta \pm \sqrt{(1+\cos\beta)^2-(2+2\cos\beta-\frac{\sin^2\beta}{\sin^2\alpha})} = 1 + \cos\beta \pm \sqrt{\frac{\sin^2 \beta}{\sin^2 \alpha} - \sin^2\beta}$$ $$= 1 + \cos\beta \pm \sin\beta\sqrt{\csc^2\alpha - 1} = 1 + \cos\beta \pm \sin\beta\cot\alpha$$ from which we choose the value of $\mu$ such that $0 \leq \lambda \leq 1$ ($\mu \geq 1$). So $\mu = 1 + \cos\beta + \sin\beta\cot\alpha$. And therefore

$$\lambda = \frac{1}{1 + \cos\beta + \sin\beta\cot\alpha}$$

If your regular polygon has side length $s$, then multiply the $\lambda$ value by $s$. It is easiest however to let $\lambda$ be between $0$ and $1$ and always move in relation to the side you are currently calculating with.

From here, the rest is quite simple. Create an array which stores lists of vertices. Iterate on the last member of this array, finding the new vertices by moving $\lambda$ along each side of the regular polygon and then append this set of new vertices to the end of the array.

Here is a sample, with $n = 7$ and $\alpha = 15$, $100$ iterations having been done. enter image description here

$n = 4$, $\alpha = 4$, $100$ iterations enter image description here


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .