计算正多边形顶点的坐标
我正在编写一个程序,其中我需要绘制任意边数的多边形,每个边都由动态变化的给定公式进行转换。其中涉及一些相当有趣的数学,但我陷入了这个问题。
如何计算正多边形(其中所有角度都相等)的顶点坐标,仅给出边数,并且理想情况下(但不一定)原点位于中心?
例如:六边形可能有以下点(都是浮点数):
( 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)
需要将坐标添加到此(或类似的东西,如列表):
Point[] _polygonVertexPoints;
I我主要对这里的算法感兴趣,但 C# 中的示例会很有用。我什至不知道从哪里开始。 我应该如何实施?这可能吗?!
谢谢。
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?
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) )
My method looks like this:
void InitPolygonVertexCoords(RegularPolygon poly)
and the coordinates need to be added to this (or something similar, like a list):
Point[] _polygonVertexPoints;
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?!
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
其中 r 是外接圆的半径。抱歉,使用了错误的语言,没有 Habla C#。
基本上,任意两个顶点之间的角度为 2 pi / n,并且所有顶点与原点的距离为 r。
编辑:
如果你想让中心位于原点以外的某个地方,请说在(x,y)
where
r
is the radius of the circumsribing circle. Sorry for the wrong language No Habla C#.Basically the angle between any two vertices is 2 pi / n and all the vertices are at distance r from the origin.
EDIT:
If you want to have the center somewher other than the origin, say at (x,y)
点的数量等于边的数量。
您需要的角度是
angle = 2 * pi / numPoints
。然后从原点上方垂直开始,多边形的大小由
半径
给出:如果您的中心是原点,则只需忽略
centreX
和centreY
> 项为 0,0。交换 cos 和 sin 将使第一个点水平指向原点右侧。
The number of points equals the number of sides.
The angle you need is
angle = 2 * pi / numPoints
.Then starting vertically above the origin with the size of the polygon being given by
radius
:If your centre is the origin then simply ignore the
centreX
andcentreY
terms as they'll be 0,0.Swapping the
cos
andsin
over will point the first point horizontally to the right of the origin.抱歉,我现在手头没有完整的解决方案,但您应该尝试寻找圆的 2D 渲染。 Circle(x,y,r) 的所有经典实现都使用与您描述的绘图类似的多边形(但具有 50 多个边)。
Sorry, I dont have a full solution at hand right now, but you should try looking for 2D-Rendering of Circles. All classic implementations of circle(x,y,r) use a polygon like you described for drawing (but with 50+ sides).
假设顶点到原点的距离为 1。并且 (1, 0) 始终是多边形的坐标。
给定顶点数(比如 n),将 (1, 0) 定位到下一个坐标所需的旋转角度将为 (360/n)。
这里需要的计算是旋转坐标。这就是它的本质; 旋转矩阵。
假设 theta = 360/n;
将是你的旋转矩阵。
如果您了解线性代数,您就已经知道我的意思了。如果不看看矩阵乘法
Say the distance of the vertices to the origin is 1. And say (1, 0) is always a coordinate of the polygon.
Given the number of vertices (say n), the rotation angle required to position the (1, 0) to the next coordinate would be (360/n).
The computation required here is to rotate the coordinates. Here is what it is; Rotation Matrix.
Say theta = 360/n;
would be your rotation matrix.
If you know linear algebra you already know what i mean. If dont just have a look at Matrix Multiplication
为正多边形生成一组坐标的一种可能的实现是:
在此实现中,我使用向量来存储生成的坐标,并使用递归函数来生成它们:
其中:
注意:
Point 是一个简单的类,用于将坐标包装到单个数据结构中:
1 就(相对于)中心,半径。在我的例子中,第一个顶点从中心向上水平平移半径长度。
2 n 正多边形有 n 个顶点。
One possible implementation to generate a set of coordinates for regular polygon is to:
In this implementation I use a vector to store the generated coordinates and a recursive function to generate them:
where:
Note:
Point is a simple class to wrap the coordinate into single data structure:
1 in terms of (relative to) the center, radius. In my case the first vertex is translated from the centre up horizontally by the radius lenght.
2 n-regular polygon has n vertices.
简单的方法是:
让我们取 N-gone(边数)和边长 L。角度将为 T = 360/N。
假设一个顶点位于原点。
你可以在for循环中做
The simple method is:
Let's take N-gone(number of sides) and length of side L. The angle will be T = 360/N.
Let's say one vertices is located on origin.
You can do in for loop
嗯,如果您测试此处列出的所有版本,您会发现实现并不好。您可以使用以下命令检查从中心到多边形每个生成点的距离: http: //www.movable-type.co.uk/scripts/latlong.html
现在我已经搜索了很多,但找不到任何好的实现来使用中心和半径计算多边形......所以我回到数学书上并尝试自己实现它。最后我想出了这个……这是 100% 好的:
如果你测试这个,你会发现所有点都在你给出的精确距离(半径)处。另外不要忘记声明 EarthRadius。
这将计算十边形的坐标。你看使用的角度是36度。您可以将 360 度分割为任意数量的边,并将结果放入角度变量中。
无论如何..我希望这对你有帮助@rmx!
hmm if you test all the versions that are listed here you'll see that the implementation is not good. you can check the distance from the center to each generated point of the polygon with : http://www.movable-type.co.uk/scripts/latlong.html
Now i have searched a lot and i could not find any good implementation for calculating a polyogon using the center and the radius...so i went back to the math book and tried to implement it myself. In the end i came up with this...wich is 100% good:
If you test this you'll see that all the points are at the exact distance that you give ( radius ). Also don't forget to declare the earthRadius.
This calculates the coordinates of a decagon. You see the angle used is 36 degrees. You can split 360 degrees to any number of sides that you want and put the result in the angle variable.
Anyway .. i hope this helps you @rmx!