作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一些用户定义的属性,然后我想用它们自动生成一个正多边形。属性是中心 x、中心 y、半径和顶点数。我想知道如何计算正多边形所有顶点的 x 和 y 坐标。我已经试过了 Calculate coordinates of a regular polygon's vertices讨论。但它总是给我错误的坐标。我目前的代码如下(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;
最佳答案
您的角度计算有误。
每个顶点之间的角度应为 2 * M_PI/vertices
。
显然你的宏:
#define DOUBLE(a) ((a)*(a))
不正确。
但是在 C++ 中,您真的不应该将宏用于此类微不足道的操作 - 它最多应该是一个内联函数,或者只是上面给出的直接公式。
关于c++ - 查找正多边形的顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6429915/
我是一名优秀的程序员,十分优秀!