1

我需要在给定 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 在这个图形基元中工作。


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

4

6 回答 6

2

Consider what 360/sides actually returns if sides is not a factor of 360 (this is integer division - see what 360/7 actually returns).

There is no need to use degrees at all - use 2*Math_PI/(double)nsides and work throughout in radians.

also you can omit the final line by using the modulus function (module nsides).

If you have more than 7 sides you will not be able to store all the points. You don't need to store all the points if you are simply drawing the polygon rather than storing it - just the last point and the current one.

于 2009-11-25T04:02:44.383 回答
1

您应该在所有计算中使用弧度。这是一个完整的程序,说明了如何最好地做到这一点:

#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在计算半径和角度时使用这些调用,因为:

  • 它们对半径没用(因为-n2= n2for all n)。
  • 它们对角度不利,因为这会迫使您进入特定的象限(错误的起点)。
于 2009-11-25T04:58:20.993 回答
0

You're trying to draw a filled poly I guess?

If you're going to try to draw the polys using a line primitive, you're going to have a lot of pain coming to you. dicroce actually gave you some very good advice on that front.

Your best bet is to find a primitive that fills for you and supply it a coordinates list. It's up to you to determine the coordinates to give it.

于 2009-11-25T04:09:02.373 回答
0

I think the main trouble is: atan2(abs(y - yc), abs(x - xc)); is giving you radians, not degrees, just convert it to degrees and try.

于 2009-11-25T04:09:17.007 回答
0
/* 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。

于 2009-11-25T04:22:06.827 回答
0

我不会只给你答案,但我有一些建议。首先,了解画线的内外工作原理。完成后,尝试编写一个填充的三角形渲染器。通常,填充的多边形一次绘制 1 条水平扫描线,从上到下。您的工作是确定每条扫描线的起始和停止 x 坐标。请注意,多边形的边缘沿着直线(提示,提示)... :)

于 2009-11-25T03:53:17.613 回答