2013-05-29 33 views
0

我想對這個分形做無限縮放。但在z軸上進行少量平移後,圖像顯示出來。 縮放鍵是f5和f6。按shift鍵進行快速縮放。 f7用於詳細說明。 Plz檢查我做錯了什麼。gluperspective image goes on dz

// mandelbrot.cpp : Defines the entry point for the console application. 

#include "stdafx.h" 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    return 0; 
} 

#include <math.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <glut.h> 
float dx=0,dy=0,dz=1.72; 
double deltax = 4/300,g=0.01;//this means 4/300 
double deltay = 4/300;// this means 4/300 
double dividecubesby = 700; 
double left = -2.0; 
double right = 2.0; 
double bottom = -2.0; 
double top = 2.0; 
int maxiteration = 90; 
float c1=0.5,c2=0.5,c3=0.5; 

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)) 
     { 
     double temp = (Zr*Zr)-(Zi*Zi); 
      Zi = 2*Zr*Zi; 
      Zr = temp+Cr; 
      Zi = Zi+Ci;    
     times = times+1; 
     } 
return times; 
} 

void display(void) 
{ 
    glLoadIdentity(); 
    glViewport(0,0,700,700); 
    glOrtho(-2.0, 2.0, -2.0, 2.0,-1,1); 
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); 
    glClear(GL_COLOR_BUFFER_BIT); 
    glColor3f(1.0, 1.0, 1.0); 
    glTranslated(dx,dy,dz); 
    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 += g) 
    { 
     for(double y= bottom; y<=top;y += g) 
     { 
      if((mandtest(x,y))==maxiteration) 
      { 
       glColor3f(c1,c2,c3); 
          glVertex2f(x,y); 
      } 

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

     } 
    } 
    glEnd(); 

glFlush(); 

} 

void init(void) 
{ 
    glClearColor(0.0, 0.0, 0.0, 0.0); 
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(45, 700/700, -100,0);  
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glOrtho(-2.0, 2.0, -2.0, 2.0,-1,1); 
} 

void processSpecialKeys(int key, int x, int y) 
{ 
    int mod = glutGetModifiers(); 
    if(key == GLUT_KEY_F1) 
    {  
     if (mod == GLUT_ACTIVE_SHIFT) 
      maxiteration=maxiteration+20; 
     maxiteration=maxiteration+5; 
     dividecubesby+=100; 

    } 
    if(key == GLUT_KEY_F2) 
    { 
     if (mod == GLUT_ACTIVE_SHIFT) 
     maxiteration=maxiteration-20; 
     maxiteration=maxiteration-5; 
     deltax=deltax*5; 
     deltay=deltay*5; 
     dividecubesby-=100; 
    } 
    if(key == GLUT_KEY_UP) 
    { 
     if (mod == GLUT_ACTIVE_SHIFT) 
     { 
      dy+=.5;top-=0.5;bottom-=0.5; 
     } 
     dy+=0.01; 
     top-=0.01; 
     bottom-=0.01; 
    } 
    if(key == GLUT_KEY_DOWN) 
    { 
     if (mod == GLUT_ACTIVE_SHIFT) 
     { 
      dy-=.5;top+=0.5;bottom+=.5; 
     } 
     dy-=0.01; 
     top+=0.01; 
     bottom+=.01; 
    } 
    if(key == GLUT_KEY_LEFT) 
    { 
     if (mod == GLUT_ACTIVE_SHIFT) 
     { 
      dx-=.5;right+=0.5;left+=0.5; 
     } 
     dx-=0.01; 
     right+=0.01; 
     left+=0.01; 
    } 
    if(key == GLUT_KEY_RIGHT) 
    { 
     if (mod == GLUT_ACTIVE_SHIFT) 
     { 
      dx+=.5;right-=0.5;left-=0.5; 
     } 
     dx+=0.01; 
     right-=0.01; 
     left-=0.01; 
    } 
    if(key == GLUT_KEY_F5) 
    { 
     if (mod == GLUT_ACTIVE_SHIFT) 
     { 
      dz+=1;left-=1.2; 
      right+=1.2; 
      top+=1.2; 
      bottom-=1.2; 
     } 
     dz+=.01; 
     left-=0.01099; 
     right+=0.01099; 
     top+=0.01099; 
     bottom-=.01099; 
    } 
    if(key == GLUT_KEY_F6) { 
     if (mod == GLUT_ACTIVE_SHIFT) 
     { 
      dz-=1;left+=1.2; 
      right-=1.2; 
      top-=1.2; 
      bottom+=1.2; 
     } 


     dz-=.01; 

     left+=0.01099; 
     right-=0.01099; 
     top-=0.01099; 
     bottom+=.01099; 
    } 

    if(key == GLUT_KEY_F4) 
    { 
     if (mod == GLUT_ACTIVE_ALT) 
     exit(0); 
    } 
    if(key == GLUT_KEY_F9) 
    { 
     c1+=0.01; 
    } 
    if(key == GLUT_KEY_F10) 
    { 
     c1-=0.01; 
    } 
    if(key == GLUT_KEY_F11) 
    { 
     c2+=0.01; 
    } 
    if(key == GLUT_KEY_F12) 
    {  
     c2-=0.01; 
    } 
    if(key == GLUT_KEY_F7) 
    { 
     g=g/2;  
    } 
    if(key == GLUT_KEY_F8) 
    { 
     g=g*2; 
    } 
    glutPostRedisplay(); 
} 

void processNormalKeys(unsigned char key, int x, int y) 
{ 
    if (key == 27) exit(0); 
} 


int main(int argc, char ** argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(dividecubesby,dividecubesby); 
    glutCreateWindow("Mandelbrot Set"); 
    init(); 
    glutDisplayFunc(display); 
    glutSpecialFunc(processSpecialKeys); 
    glutKeyboardFunc(processNormalKeys); 
    glutMainLoop(); 
    return 0; 
} 

回答

0

你幾乎可以肯定跑出來的精密位 - 雙有53位,足以放大到最多約2^-43或在1,024像素的窗口1E-13 - 超出你不t甚至有足夠的位來區分不同像素的C值。當您放大「太遠」時,會導致粒狀矩形。

您可能會看看libqd或libmpfr或其他更高精度的數值庫,它們會爲您提供更多位的浮點數。儘管如此,它們的速度要慢很多。