用OpenGL绘制Fermat螺旋

时间:2012-09-03 20:45:48

标签: c opengl spiral

我正在使用OpenGL绘制此方法,绘图是2D。

我知道这个理论,你可以在维基百科中找到这个定义,但我不知道我做错了什么。问题是当我使用平方根的负解法绘制点时。

//-----------------------------------------
//           ESPIRAL DE FERMAT
//-----------------------------------------
// float a --> x-inicio
// float b --> y-inicio
// float thetaStart --> angulo de comienzo
// float thetaEnd --> angulo de fin.
// unsigned int samples --> número de muestras, por defecto 200.
//------------------------------------------------------------------
void glFermatSpiral(float a, float b, float thetaStart, float thetaEnd, unsigned int samples = 200 )
{
    glBegin( GL_LINE_STRIP );

    float dt = (thetaEnd - thetaStart) / (float)samples;

    for( unsigned int i = 0; i <= samples; ++i )
    {
        // archimedean spiral
        float theta = thetaStart + (i * dt);
        // Specific to made a Fermat Spiral.
        float r = sqrt( theta );

        // polar to cartesian
        float x = r * cos( theta );
        float y = r * sin( theta );

        // Square root means two solutions, one positive and other negative. 2 points to be drawn.
        glVertex2f( x, y );

        x = -r * cos( theta );
        y = -r * sin( theta );

        glVertex2f( x, y );
    }

    glEnd();
}

这就是我调用此方法的方式,并定义了我的绘图空间。

glFermatSpiral(0.05, 0.2, 1.0, 25.0);

gluOrtho2D(-4, 4, -4, 4);   //  left, right, bottom, top

这就像解决方案一样。 enter image description here

1 个答案:

答案 0 :(得分:4)

您正在使用线条绘制点,但是您会在螺旋的正面和负面之间来回反复。

除非您希望在这些点之间绘制线条,否则不应将它们按顺序放在条带中。

我建议绘制所有正解的一条线条,然后用所有负解开始新的条带。你需要单独绘制线条。

相关问题