2012-10-23 49 views
0

我試圖創建一個使用C++/OpenGL的一個天壤之別的形象。下面是我有這麼遠過來說出來的簡單的代碼和圖像:我Mandelbrot集有線輸出圖像

enter image description here

#include<GL/gl.h> 
#include <GL/glu.h> 
#include <GL/glut.h> 
#include <math.h> 
#include <stdio.h> 
#include <stdlib.h> 

double dividecubesby = 300; 
const double left = -2.0; 
const double right = 2.0; 
const double bottom = -2.0; 
const double top = 2.0; 
int maxiteration = 90; 

int mandtest(double Cr, double Ci){ 

    double Zr = 0.0; 
    double Zi = 0.0; 
    int times = 0; 

    Zr = Zr+Cr; 
    Zi = Zi+Ci; 

     while ((((Zr*Zr)+(Zi*Zi))<4) && (times < maxiteration)){ 


     Zr = (Zr*Zr)-(Zi*Zi); 
     Zi = 2*Zr*Zi; 
     Zr = Zr+Cr; 
     Zi = Zi+Ci;     
     times = times+1; 

     } 
return times; 



void display(void) 
{ 
     glClear(GL_COLOR_BUFFER_BIT); 
     glColor3f(1.0f,1.0f,1.0f); 
     double real = left;//this is the real part of the order-pair in the cube 
     double image = top;// this is the image part of the order-pair in the cube 
     double deltax = ((right - left)/(dividecubesby));//this means 4/300 
     double deltay = ((top- bottom)/(dividecubesby));// this means 4/300 

glBegin(GL_POINTS); 

    for(double x= left;x<=right;x += deltax){ 


     for(double y= bottom; y<=top;y += deltay){ 

      if((mandtest(x,y))==maxiteration){ 

       glColor3f(1.0f,1.0f,1.0f); 
          glVertex2f(x,y); 

      } 

      else { 
       glColor3f(0.0f,0.0f,(float)mandtest(x,y)/maxiteration); 
            glVertex2f(x,y); 
      } 

     } 
    } 
    glEnd(); 

glFlush(); 

} 

void init(void) 
{ 
//select clearing (background) color 
    glClearColor(0.0, 0.0, 0.0, 0.0); 

     //initialize viewing values 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
//glortho(left,right,bottom,top,near,far) 
    gluOrtho2D(-2.0, 2.0, -2.0, 2.0); 
} 


int main(int argc, char ** argv) 
{ 
glutInit(&argc, argv); 
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
glutInitWindowSize(dividecubesby,dividecubesby); 
glutCreateWindow("A Simple OpenGL Windows Application with GLUT"); 
init(); 
glutDisplayFunc(display); 
glutMainLoop(); 


    return 0; 
} 
+0

我與Java做到了:http://craicpropagation.blogspot.com/2011/03/mandelbrot-set.html。查看代碼並查看它是否對您有所幫助。 – duffymo

+2

如果你不想手動處理實部和虛部,C++有std :: complex。 – bames53

+0

庫爾bames我一定會嘗試.. – solti

回答

4

Zr = (Zr*Zr)-(Zi*Zi); 
    Zi = 2*Zr*Zi; 
    Zr = Zr+Cr; 
    Zi = Zi+Ci; 

您使用新Zr計算Zi,即產生錯誤的結果,使其成爲

double temp = (Zr*Zr)-(Zi*Zi); 
    Zi = 2*Zr*Zi; 
    Zr = temp+Cr; 
    Zi = Zi+Ci; 

,以獲得更好的結果:

enter image description here

+0

我試過了,但仍然是相同的圖像... :(謝謝大家的響應想到:) – solti

+1

嗯,嗯,這是明顯的錯誤。如果這沒有幫助(你應該做出這樣的改變,否則你將不會得到一個錯誤的迭代正確的圖片),必須有別的東西。讓我們看看我是否找到了一些東西。 –

+0

@gunda糟糕,我犯了同樣的錯誤,我太替換了,很開心。現在修好了,它產生了足夠好的照片:) –