我正在编写一个程序,在该程序中我需要绘制任意数量的边的多边形,每个边都由一个动态变化的给定公式转换.涉及一些相当有趣的数学,但我被困在这个问题上.
I am writing a program in which I need to draw polygons of an arbitrary number of sides, each one being translated by a given formula which changes dynamically. There is some rather interesting mathematics involved but I am stuck on this probelm.
如何计算正多边形(所有角度都相等)的顶点坐标,仅给出边数,理想情况下(但不是必须)具有原点在中心?
How can I calculate the coordinates of the vertices of a regular polygon (one in which all angles are equal), given only the number of sides, and ideally (but not neccessarily) having the origin at the centre?
例如:一个六边形可能有以下几点(都是float
s):
For example: a hexagon might have the following points (all are float
s):
( 1.5 , 0.5 *Math.Sqrt(3) )
( 0 , 1 *Math.Sqrt(3) )
(-1.5 , 0.5 *Math.Sqrt(3) )
(-1.5 , -0.5 *Math.Sqrt(3) )
( 0 , -1 *Math.Sqrt(3) )
( 1.5 , -0.5 *Math.Sqrt(3) )
我的方法是这样的:
void InitPolygonVertexCoords(RegularPolygon poly)
并且需要将坐标添加到此(或类似的东西,如列表):
and the coordinates need to be added to this (or something similar, like a list):
Point[] _polygonVertexPoints;
我主要对这里的算法感兴趣,但 C# 中的示例会很有用.我什至不知道从哪里开始.我应该如何实现它?有可能吗?!
I'm interested mainly in the algorithm here but examples in C# would be useful. I don't even know where to start. How should I implement it? Is it even possible?!
谢谢.
for (i = 0; i < n; i++) {
printf("%f %f
",r * Math.cos(2 * Math.PI * i / n), r * Math.sin(2 * Math.PI * i / n));
}
其中 r
是外接圆的半径.对不起,错误的语言没有 Habla C#.
where r
is the radius of the circumsribing circle. Sorry for the wrong language No Habla C#.
基本上任意两个顶点之间的夹角是2 pi/n,并且所有顶点距离原点r.
Basically the angle between any two vertices is 2 pi / n and all the vertices are at distance r from the origin.
如果你想让中心在原点以外的地方,比如 (x,y)
If you want to have the center somewher other than the origin, say at (x,y)
for (i = 0; i < n; i++) {
printf("%f %f
",x + r * Math.cos(2 * Math.PI * i / n), y + r * Math.sin(2 * Math.PI * i / n));
}
这篇关于计算正多边形顶点的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!