How to find the side-length of a regular polygon given number of sides and diameter

62 Views Asked by At

For a program that I am writing, I need a function that takes in the number of sides, and the diameter, and will output side length. I have tried to ask my teachers, but none of them had time to figure it out.

I first tried this equation, d being the diameter (from the center to a vertex) and n being the number of sides:
$$ d\cdot2\cdot\tan\left(\frac{n}{180}\right) $$

That is the only thing that I really tried. I also tried a scaling factor, but that would never work for all values of $n$. I could not find anything else that looked like it would work.