gpt4 book ai didi

c++ - 查找正多边形的顶点

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:29:28 25 4
gpt4 key购买 nike

我有一些用户定义的属性,然后我想用它们自动生成一个正多边形。属性是中心 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com