How to draw a regular polygon so that one edge is parallel to the x axis?

I know that to draw a regular polygon from a center point, you use something line by line:

for (int i = 0; i < n; i++) { p.addPoint((int) (100 + 50 * Math.cos(i * 2 * Math.PI / n)), (int) (100 + 50 * Math.sin(i * 2 * Math.PI / n)) ); } 

However, is there a way to change this code (without adding turns) to make sure the polygon is always drawn so that the top or bottom edge is 180 degrees parallel to the line? For example, as a rule, the code above for a pentagon or square (where n = 5 and 4, respectively) will produce something like:

http://i.stack.imgur.com/Nv6Xf.gifhttp://i.stack.imgur.com/or967.gif

When I search:

http://i.stack.imgur.com/Tfxs1.gifhttp://i.stack.imgur.com/BIORA.gif

Is there any mathematical way to do this?

+4
source share
2 answers

You need to add Pi/2-Pi/n

 k[n_] := Pi/2 - Pi/n; f[n_] := Line[ Table[50 {Cos[(2 i ) Pi/n + k[n]] ,Sin[(2 i) Pi/n + k[n]]}, {i,0,n}]]; GraphicsGrid@Partition [Graphics /@ Table[f[i], {i, 3, 8}], 3] 

enter image description here

Edit

In response to your comment, I will explain how I came to the formula. Take a look at the following image:

enter image description here

As you can see, we want the midpoint of the side aligned with Pi / 2. So ... what is ? ? It is obvious

2? = 2 Pi / n (one side) β†’? = Pi / n

Edit 2

If you want the bottom side aligned with the x axis, add 3 Pi/2- Pi/n instead ...

enter image description here

+7
source

Add Math.PI / n to the corners.

0
source

Source: https://habr.com/ru/post/1345634/


All Articles