【发布时间】:2010-12-20 03:38:22
【问题描述】:
我需要在给定 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 处理这个图形基元。
编辑:我不想填充多边形,只是绘制它。
【问题讨论】:
-
你能澄清一下你是想要一个填充的多边形(在这种情况下,关于三角形的答案可能是相关的)还是一个未填充的多边形(你只需要你建议的周边线,而你只需要画线吗?需要
-
尝试写一个填充三角形渲染器。通常,填充的多边形一次绘制 1 条水平扫描线,从上到下。您的工作是确定每条扫描线的起始和停止 x 坐标。请注意,多边形的边沿直线(提示,提示)。
标签: c graphics polygon primitive atan2