gpt4 book ai didi

c - 在C中绘制多边形

转载 作者:太空宇宙 更新时间:2023-11-04 02:20:01 25 4
gpt4 key购买 nike

我需要在给定 2 个点(中心点和顶点的 1 个点)的情况下绘制一个“n”条边的多边形,只是我的数学很烂。我已经阅读了很多书,所有这些都是我能够理解的(我不知道它是否正确):

好的,我用毕达哥拉斯定理计算两点之间的距离(半径):

sqrt(pow(abs(x - xc), 2) + pow(abs(y - yc), 2));

以及这 2 个点与 atan2 之间的角度,如下所示:

atan2(abs(y - yc), abs(x - xc));

其中 xc, yc 是中心点,x, y 是唯一知道的顶点。

使用这些数据我可以:

void polygon(int xc, int yc, int radius, double angle, int sides)
{
int i;
double ang = 360/sides; //Every vertex is about "ang" degrees from each other
radian = 180/M_PI;
int points_x[7]; //Here i store the calculated vertexs
int points_y[7]; //Here i store the calculated vertexs

/*Here i calculate the vertexs of the polygon*/
for(i=0; i<sides; i++)
{
points_x[i] = xc + ceil(radius * cos(angle/radian));
points_y[i] = yc + ceil(radius * sin(angle/radian));
angle = angle+ang;
}

/*Here i draw the polygon with the know vertexs just calculated*/
for(i=0; i<sides-1; i++)
line(points_x[i], points_y[i], points_x[i+1], points_y[i+1]);
line(points_y[i], points_x[i], points_x[0], points_y[0]);
}

问题是程序无法正常工作,因为它绘制的线条不像多边形。

有人怎么知道足够多的数学来帮忙?我正在使用 C 和 turbo C 处理这个图形原语。


编辑:我不想填充多边形,只是绘制它。

最佳答案

如果 sides 不是 360 的因数,请考虑 360/sides 实际返回的内容(这是整数除法 - 查看 360/7 实际返回的内容)。

根本不需要使用度数 - 使用 2*Math_PI/(double)nsides 并始终以弧度表示。

您也可以使用取模函数(module nsides)省略最后一行。

如果您有超过 7 个面,您将无法存储所有点。如果您只是绘制多边形而不是存储多边形,则不需要存储所有点 - 只需最后一个点和当前点。

关于c - 在C中绘制多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1794489/

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