gpt4 book ai didi

c - 使用 OpenGL 绘制费马螺旋

转载 作者:行者123 更新时间:2023-12-04 18:12:58 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com