1

我正在使用 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 是像素坐标

4

1 回答 1

2

你的变量太多了。什么定义了图像的宽度?(right - left)? baseSize + f(zoomLevel)? SizeReal? 目前尚不清楚谁的工作是设置谁以及谁由谁使用,因此您不能希望始终如一地更新所有内容。

另外,为什么dividecubesby每次缩放图像大小都会增加 500 倍?定义单击坐标限制的窗口系统窗口的宽度/高度在哪里?

我的建议是从头开始,也许画一张谁更新谁的图表(left/right -> imageWidth)。确保您获得正确的单击坐标,而与您的绘图窗口(左/右/上/下)无关,然后从那里继续。事实上,我认为您的第一次缩放是偶然正常工作的。

于 2012-11-08T19:34:39.197 回答