2012-09-03 54 views
2

我正在做一個使用OpenGL繪製這個的方法,繪圖是2D的。使用OpenGL繪製費馬螺旋

我知道理論,你可以在維基百科找到定義,但我不知道自己做錯了什麼。問題是當我使用平方根的負解來繪製點時。

//----------------------------------------- 
//   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

+0

究竟是什麼問題? – Tim

+0

這種方法繪製了一些非常奇怪的東西,它們之間的線交叉,而不是想要的解決方案。 –

回答

4

您正在使用線條繪製點,但您在螺旋線的正面和負面之間來回跳動。

除非你想要在這些點之間畫一條線,否則你不應該把它們按順序放在一個條上。

我建議繪製所有正解的一條線條,然後用所有負解開始一條新線條。你需要分別畫線。