查找正多边形的顶点
我有一些由用户定义的属性,然后我想使用它们自动生成正多边形。属性包括中心 x、中心 y、半径和顶点数。我想知道如何计算正多边形所有顶点的x和y坐标。我已经尝试过计算正多边形顶点的坐标 讨论。但它总是给我错误的坐标。我当前的代码如下(C++):
#define DOUBLE(a) ((a)*(a))
...
if(radius <= 0 || vertices < 3)
return NULL;
Polygon* poly = new Polygon;
double angle = DOUBLE(M_PI) / vertices;
for(long i = 0; i < vertices; i++)
{
double a = (angle * i);
poly->add(centerX + radius * cos(a), centerY + radius * sin(a));
}
return poly;
I have some properties defined by the user, and then I want use them to automatically generate a regular polygon. The properties are centre x, centre y, radius and number of vertices. I would like to know how to calculate the x and y coordinates of all vertices of a regular polygon. I've already tried to do as Calculate coordinates of a regular polygon's vertices discussion. But it always gives me thw wrong coordinates. My current code is as follows (C++):
#define DOUBLE(a) ((a)*(a))
...
if(radius <= 0 || vertices < 3)
return NULL;
Polygon* poly = new Polygon;
double angle = DOUBLE(M_PI) / vertices;
for(long i = 0; i < vertices; i++)
{
double a = (angle * i);
poly->add(centerX + radius * cos(a), centerY + radius * sin(a));
}
return poly;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的角度计算有错误。
每个顶点之间的角度应为
2 * M_PI / vertices
。显然你的宏:
不正确。
然而,在 C++ 中,无论如何,您确实不应该使用宏来进行此类琐碎的操作 - 它最多应该是一个内联函数,或者只是上面给出的直接公式。
There's an error in your angle calculation.
The angle between each vertex should be
2 * M_PI / vertices
.Obviously your macro:
is incorrect.
However in C++ you really shouldn't use macros for such trivial operations anyway - it should at the very most be an inline function, or just the direct formula given above.
尝试一下
,或者
您正在以虚假身份定义 SQUARE(a) 。
Try
or
You are defining SQUARE(a) under a false identity.