2014-02-28 114 views
1

我想顯示mandelbrot集分形使用c編程,openGL,在linux上過剩。 這是我的代碼。它只在中心顯示一個點。無法弄清楚我出錯的地方。任何幫助?c編程 - Mandelbrot集

#include <GL/glut.h> // Header File For The GLUT Library 
#include <math.h> 
#include <complex.h> 

int main (int argc, char *argv[]) 
{ 
    int i, j, count; 
    float re_max=1.0, re_min=-2.0, im_max=1.0, im_min=-1.0 ; //aspect ratio 3/2 
    float real_delta = (re_max - re_min)/750; 
    float imag_delta = (im_max - im_min)/500; 

    double complex x = 0.0 + 0.0 * I; 
    double complex z = re_min + im_min * I; 

    double absolute_x; 

    glutInit(&argc, argv); 
    glutInitDisplayMode (GLUT_RGBA | GLUT_DEPTH); 
    glutInitWindowPosition(150,150); 
    glutInitWindowSize(750,500); // aspect ratio of 3/2 
    glutCreateWindow (argv[0]); 

    glClearColor (0.1, 0.2, 0.3, 0.0); // choosing the background color 
    glClear (GL_COLOR_BUFFER_BIT);  // setting the color buffer to background color 
    glColor4f(0.5,0.7,0.3,0.0); 
    glPointSize(1); 

    for (i=0, z = re_min + im_min * I ; i<750; i++, z = (re_min + real_delta) + im_min * I) 
     { 

     for (j=0, z = creal(z) + im_min * I; j<500; j++, z = creal(z) + (im_min + imag_delta) * I) 
      { 
      count = 0; 
      x = 0 + 0*I; 

      while ((absolute_x = fabs(x))<=2.0 && count < 64) 
      { 
       x = (x * x) + z; 
       count++; 
      } 

       if (absolute_x <= 2.0){ 
        glBegin(GL_POINTS); 
         glVertex2i(i,j); 
        glEnd(); 
       } 
      } 
     } 

    glFlush(); 

    glutMainLoop(); 

    return (0); 

} 

由於編碼問題解決了,我在編輯這個問題與所產生的分形:-)美的畫卷沿「正確的工作代碼」

Mandelbrot set fractal

這裏是工作代碼:

#include <GL/glut.h> // Header File For The GLUT Library 
#include <math.h> 
#include <complex.h> 

int main (int argc, char *argv[]) 
{ 
    int i, j, count; 
    float re_max=1., re_min=-2.0, im_max=1.0, im_min=-1.0 ; //aspect ratio 3/2 
    float real_delta = (re_max - re_min)/750; 
    float imag_delta = (im_max - im_min)/500; 

    double complex x = 0.0 + 0.0 * I; 
    double complex z = re_min + im_min * I; 

    double absolute_x; 

    glutInit(&argc, argv); 
    glutInitDisplayMode (GLUT_RGBA | GLUT_DEPTH); 
    glutInitWindowPosition(150,150); 
    glutInitWindowSize(750,500); // aspect ratio of 3/2 
    glutCreateWindow (argv[0]); 

    glClearColor (0.5, 0.2, 0.3, 0.0); // choosing the background color 
    glClear (GL_COLOR_BUFFER_BIT);  // setting the color buffer to background color 
    glColor4f(0.5,0.7,0.3,0.0); 
    glPointSize(1); 

    for (i=0, z = re_min + im_min * I ; i<750; i++, z = (creal(z) + real_delta) + im_min * I) 
     { 

     for (j=0, z = creal(z) + im_min * I; j<500; j++, z = creal(z) + (cimag(z) + imag_delta) * I) 
      { 
      count = 0; 
      x = 0 + 0*I; 

      while ((absolute_x = fabs(x))<=2.0 && count < 64) 
      { 
       x = (x * x) + z; 
       count++; 
      } 

       if (absolute_x <= 2.0){ 
        glBegin(GL_POINTS); 
         glVertex2f(i/750., j/500.);; 
        glEnd(); 
       } 
      } 
     } 

    glFlush(); 

    glutMainLoop(); 

    return (0); 
} 
+0

'absolute_x = cabs(x);'或''? – chux

+0

「它只在中心顯示一個點。」我已經編譯並運行你的代碼,並且至少在我的筆記本電腦上(Linux x86_64)從未調用過對'glBegin'的調用。 – starrify

+0

你的循環看起來很奇怪:在'i'的每個循環中你都有一個相同的'z',並且我認爲你沒有從'j'的循環中得到正確的'z' – starrify

回答

3

這裏有兩種主要的失誤:

  1. 你的循環看起來很奇怪。 i的循環不會在其每次迭代中更改z的值,因此j也是如此。
    建議:不更新的循環語句這將只會使事情複雜z,而不是直接計算它的循環體是這樣的:

    for (i=0; i<750; i++) 
    { 
        for (j=0; j<500; j++) 
        { 
         z = re_min + real_delta * i + (im_min + imag_delta * j) * I; 
         /* other of your codes */ 
        } 
    } 
    
  2. 您沒有使用OpenGL正確的方式。通過調用glVertex2i(i, j);即可指定座標x=iy=j,但OpenGL的默認視口爲(-1,1)x(-1,1)
    我這裏還有兩種解決方案,你可以使用:

    • 放置一個glScalef創建的窗口來指定座標刻度後不改變你的代碼的其他部分:

      /* ... */ 
      glutCreateWindow(argv[0]); 
      glScalef(1./750., 1./500., 1.); 
      /* ... */ 
      
    • 通過縮放座標:

      /* don't use this line of your code: */ 
      /* glVertex2i(i, j); */ 
      /* while use this line: */ 
      glVertex2f(i/750., j/500.); 
      
+0

非常感謝..美麗的分形致力於你! – KawaiKx

+0

@Saurabh非常榮幸能夠提供幫助。 :D – starrify

+0

@Saurabh也許發佈分形圖片? – chux