我正在做一个使用 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
这就像解决方案一样。
最佳答案
您使用线条来绘制点,但您不断在螺旋的正侧和负侧之间来回弹跳。
除非您希望在这些点之间绘制一条线,否则不应将它们按顺序放在 strip 中。
我建议绘制一条包含所有正解的线带,然后开始绘制一条包含所有负解的新线带。您需要单独绘制线条。
关于c - 用OpenGL绘制费马螺线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12253832/