【问题标题】:my mandelbrot set outputs wired image我的 mandelbrot 集输出有线图像
【发布时间】:2012-10-23 10:04:40
【问题描述】:

我正在尝试使用 c++/opengl 创建一个 mandelbrot 图像。下面是我到目前为止想出的简单代码和出来的图像:

 #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;
}

【问题讨论】:

标签: c++ c algorithm opengl


【解决方案1】:

在

    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;

为了获得更好的结果:

【讨论】:

  • 我试过了,但仍然是相同的图像...... :( 谢谢你的回应 :)
  • 啊,那是明显的错误。如果这没有帮助(尽管如此,您应该进行更改,否则您将永远无法通过错误的迭代获得正确的图片),那么肯定还有其他原因。让我们看看我是否找到了什么。
  • @gunda 哎呀,我犯了同样的错误,我太高兴了。现在修复了,它产生了一张足够漂亮的图片:)
  • 现在进行一些逃生时间着色。
  • OpenMP 会给您带来轻松但显着的性能提升。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-13
  • 1970-01-01
  • 2019-04-22
相关资源
最近更新 更多