缩放mandelbrot设置第二次不让它放大所需的地方

时间:2012-11-06 15:25:47

标签: c++ c opengl mandelbrot

我正在使用opengl / c ++来绘制mandelbrot集并尝试放大。我能够第一次缩放并缩放我想要的地方(通过点击),但是当我下次尝试缩放时它不会缩放我想要缩放的地方而是它移动和缩放距离我想要的地方有点远放大。 我用

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

double dividecubesby  = 700;
double left = -2.0;
double right = 2.0;
double bottom = -2.0;
double top = 2.0;
int maxiteration = 128;
int zoomlevel = 3;
double baseSize = 4.0;
double Size = 0.0;
double xco=0.0;
double yco=0.0;

    void   SetXYpos(int px,int py)
    {
        xco = left+(right-left)*px/dividecubesby;
        yco = top-(top-bottom)*py/dividecubesby;
    }

   void keyPressed(unsigned char key, int x, int y)
   {

       int xx= x;
       int yy= y;
       setXYpos(xx,yy);


       Size = 0.5*(pow(2.0, (-zoomlevel)));



       switch(key){

       case 'z':

      left   =  xco -  Size;
      right  =  xco +  Size;
      bottom =  yco -  Size;
      top    =  yco +  Size;

      dividecubesby = dividecubesby+100;        
      maxiteration  = maxiteration+100; 
  zoomlevel=zoomlevel+1;

      glutPostRedisplay();
      break;
      }

  }

    int mandtest(double Cr, double Ci)
 {


    double Zr = 0.0;
    double Zi = 0.0;
    int times = 0;
    double temp;
    Zr = Zr+Cr;
    Zi = Zi+Ci;

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

       temp = (Zr*Zr)-(Zi*Zi);
       Zi = 2*Zr*Zi;

       Zr = temp+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 deltax = ((right - left)/(dividecubesby-1));
    double deltay = ((top- bottom)/(dividecubesby-1));

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluOrtho2D(left,right,bottom,top);


    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((float)mandtest(x,y)/10,0.0f,(float)mandtest(x,y)/30);
                    glVertex2f(x,y);
            }

        }
     }
    glEnd();

glFlush();
}

计算点击鼠标的位置笛卡尔坐标的中间位置[-2,2]

px和py是像素坐标

1 个答案:

答案 0 :(得分:2)

你有太多的变数。什么定义了图像的宽度? (right - left)baseSize + f(zoomLevel)SizeReal?目前尚不清楚谁设置谁和谁由谁使用,所以你不能希望一直更新所有内容。

另外,为什么dividecubesby增加了500,而每次缩放时图像尺寸减半?窗口系统窗口的宽度/高度在哪里定义了点击坐标的限制?

我的建议是从头开始,可能会绘制一个更新谁(left/right -> imageWidth)的图表。确保获得正确的单击坐标,与绘图窗口(左/右/上/下)无关,并从那里继续。事实上,我认为你的第一个缩放是偶然的。