如何绘制一个正多边形,使其一条边平行于 X 轴?
我知道要从中心点绘制正多边形,您可以使用以下内容:
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))
);
}
但是,无论如何都可以更改此代码(不添加旋转)以确保始终绘制多边形,以便最顶部或最底部边与 180 度线平行吗?例如,通常情况下,上面的五边形或正方形(其中 n = 5 和 4 分别)的代码将生成如下内容:
当我要查找的是:
有什么数学方法可以实现这一点吗?
I know that to draw a regular polygon from a center point, you use something along the lines of:
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 anyway to change this code (without adding rotations ) to make sure that the polygon is always drawn so that the topmost or bottommost edge is parallel to a 180 degree line? For example, normally, the code above for a pentagon or a square (where n = 5 and 4 respectively) would produce something like:
When what I'm looking for is:
Is there any mathematical way to make this happen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须添加
Pi/2-Pi/n
编辑
回答你的评论,我将解释我是如何得出这个公式的。看下图:
正如您所见,我们希望边的中点与 Pi 对齐/2。那么...α是什么?很明显
编辑 2
如果您希望底边与 x 轴对齐,请添加
3 Pi/2- Pi/n
...You have to add
Pi/2-Pi/n
Edit
Answering your comment, I'll explain how I arrived at the formula. Look at the following image:
As you may see, we want the middle point of a side aligned with Pi/2. So ... what is α? It's obvious
Edit 2
If you want the bottom side aligned with the x axis, add
3 Pi/2- Pi/n
instead ...将 Math.PI / n 添加到角度中。
Add Math.PI / n to the angles.