c - 用OpenGL绘制费马螺线

标签 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

最佳答案

您使用线条来绘制点,但您不断在螺旋的正侧和负侧之间来回弹跳。

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

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

关于c - 用OpenGL绘制费马螺线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12253832/

相关文章:

c - 如何修复以下 C 程序的输出?

c - 如何插值 HSV 颜色?

vba - 我怎样才能编写产生螺旋的代码?

c - 螺旋矩阵(作为二维指针)

python - 使用双曲正切在 Python 中创建螺旋结构

c - Xcode5与C失去连接错误

const int *ptr=500;它到底存储在哪里

java - 如何在 JavaFX 中禁用或绕过硬件图形加速(Prism)

c++ - OpenGL 顶点数组球体杂散顶点

c++ - 着色器纹理值与创建纹理时写入的值不同