0

我想对这个分形进行无限放大。但在 z 轴上进行几次平移后,图像 dis 出现。缩放键是 f5 和 f6。按 shift 进行快速缩放。f7 用于详细说明。请检查我做错了什么。

// 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;
}
4

1 回答 1

0

您几乎可以肯定精度不足 - double 有 53 位,足以在 1024px 窗口中放大至最多 2^-43 或 1e-13 - 除此之外,您甚至没有足够的位来区分不同像素的 C 值。当您放大“太远”时,这会导致颗粒状的矩形。

您可能会查看 libqd 或 libmpfr 或其他更高精度的数值库,它们为您提供更多位的浮点。不过,它们的速度要慢得多。

于 2013-06-19T12:32:15.340 回答