【问题标题】:Draw Fermat Spiral with OpenGL使用 OpenGL 绘制费马螺旋
【发布时间】:2012-09-03 20:45:48
【问题描述】:

我正在做一个使用 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

就像解决方案一样。

【问题讨论】:

  • 这个方法画了一些很奇怪的东西,它们之间的线交叉,不像想要的解决方案。

标签: c opengl spiral


【解决方案1】:

您正在使用线带来绘制点,但您一直在螺旋的正面和负面之间来回弹跳。

除非你想在这些点之间画一条线,否则你不应该把它们按顺序排列成一条带。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    相关资源
    最近更新 更多