[英]Draw a polygon in C
我需要给定2个点(他的顶点的中心和1个点)来绘制“ n”边的多边形,这只是我在数学上很烂。 我读了很多书,这就是我能够弄清楚的东西(我不知道它是否正确):
好的,我用毕达哥拉斯定理求出两个点(半径)之间的距离:
sqrt(pow(abs(x - xc), 2) + pow(abs(y - yc), 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
并以弧度为单位进行工作。
您也可以使用模数函数(模块nsides)省略最后一行。
如果边数超过7,则将无法存储所有点。 如果您只是绘制多边形而不是存储多边形,则无需存储所有点-只需存储最后一个点和当前点即可。
您应该在所有计算中使用弧度。 这是一个完整的程序,说明了如何最好地做到这一点:
#include <stdio.h>
#define PI 3.141592653589
static void line (int x1, int y1, int x2, int y2) {
printf ("Line from (%3d,%3d) - (%3d,%3d)\n", x1, y1, x2, y2);
}
static void polygon (int xc, int yc, int x, int y, int n) {
int lastx, lasty;
double r = sqrt ((x - xc) * (x - xc) + (y - yc) * (y - yc));
double a = atan2 (y - yc, x - xc);
int i;
for (i = 1; i <= n; i++) {
lastx = x; lasty = y;
a = a + PI * 2 / n;
x = round ((double)xc + (double)r * cos(a));
y = round ((double)yc + (double)r * sin(a));
line (lastx, lasty, x, y);
}
}
int main(int argc, char* argv[]) {
polygon (0,0,0,10,4); // A diamond.
polygon (0,0,10,10,4); // A square.
polygon (0,0,0,10,8); // An octagon.
return 0;
}
哪个输出(这里没有精美的图形,但是您应该有个主意):
===
Line from ( 0, 10) - (-10, 0)
Line from (-10, 0) - ( 0,-10)
Line from ( 0,-10) - ( 10, 0)
Line from ( 10, 0) - ( 0, 10)
===
Line from ( 10, 10) - (-10, 10)
Line from (-10, 10) - (-10,-10)
Line from (-10,-10) - ( 10,-10)
Line from ( 10,-10) - ( 10, 10)
===
Line from ( 0, 10) - ( -7, 7)
Line from ( -7, 7) - (-10, 0)
Line from (-10, 0) - ( -7, -7)
Line from ( -7, -7) - ( 0,-10)
Line from ( 0,-10) - ( 7, -7)
Line from ( 7, -7) - ( 10, 0)
Line from ( 10, 0) - ( 7, 7)
Line from ( 7, 7) - ( 0, 10)
我已经按照您的原始规范编写了polygon
函数,只传递了两个坐标。 顺便说一句,您不希望在计算半径和角度时使用这些abs
调用,因为:
-n
2
= n
2
对于所有n
)。 我不会只给你答案,但是我有一些建议。 首先,了解画线在内部和外部的工作方式。 遇到这种情况时,请尝试编写一个填充的三角形渲染器。 通常,填充的多边形从上到下每次绘制1条水平扫描线。 您的工作是确定每条扫描线的起点和终点x坐标。 请注意,多边形的边缘遵循一条直线(提示,提示)... :)
我想您正在尝试绘制填充的多边形吗?
如果您要尝试使用线图元绘制多边形,将会遇到很多麻烦。 dicroce实际上在这方面给了您一些很好的建议。
最好的选择是找到一个适合您的图元并为其提供坐标列表。 由您决定要给出的坐标。
我认为主要的麻烦是: atan2(abs(y - yc), abs(x - xc));
给您弧度,而不是度数,只需将其转换为度数并尝试。
/* all angles in radians */
double ainc = PI*2 / sides;
int x1, y1;
for (i = 0; i <= sides; i++){
double a = angle + ainc * i;
int x = xc + radius * cos(a);
int y = yc + radius * sin(a);
if (i > 0) line(x1, y1, x, y);
x1 = x; y1 = y;
}
或者,您可以将点保存在数组中,如果有的话可以调用DrawPoly例程。
如果要填充的多边形,请调用FillPoly。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.