gpt4 book ai didi

c++ - 我的 mandelbrot 集输出有线图像

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:39:24 24 4
gpt4 key购买 nike

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

enter image description here

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

最佳答案

    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;

为了获得更好的结果:

enter image description here

关于c++ - 我的 mandelbrot 集输出有线图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13022305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com