Mandelbrot和Julia
概述
mandelbrot
julia
Mandelbrot
对全体复数z,满足xn+1 = xn2 + z从x0 = 0起,|x|随n值增加不趋于无穷大,则z属于Mandelbrot集
代码
#include <glut.h> int g_width, g_height; /********************************* input: z = a + bi MaxIteration,迭代次数上限 output: 灰度级n *********************************/ int GrayLevel(double a, double b, int MaxIteration) { double x = 0, y = 0 , xx = 0, yy = 0; int n = 0; while(n++ < MaxIteration) { y = 2 * x * y + b; x = xx - yy + a; xx = x * x; yy = y * y; if ((xx + yy) > 4) { return n; } } return 0; } void myDisplay() { double min_a, max_a, min_b, max_b, step_a, step_b, a, b; int MaxIteration = 64; int Iteration; float scale = 255.0 / MaxIteration; int x,y; min_a = -2.0; max_a = 0.5; min_b = -1.25; max_b = 1.25; step_a = (max_a - min_a) / g_width; step_b = (max_b - min_b) / g_height; glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POINTS); a = min_a; for (x = 0; x < g_width; x++) { b = min_b; for (y = 0; y < g_height; y++) { Iteration = 255 - scale * GrayLevel(a, b, MaxIteration); glColor3ub(Iteration, Iteration, Iteration); glVertex2i(x, y); b += step_b; } a += step_a; } glEnd(); glFlush(); } void reshape(GLsizei width, GLsizei height) { float w_aspect = 4.0 / 3.0; float aspect = ((float)width) / height; g_width = width; g_height = height; if (aspect <= w_aspect) { glViewport(0, (height - width / w_aspect) / 2, width, width / w_aspect); } else glViewport((width - height * w_aspect) / 2, 0, height * w_aspect, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-0.0, 400.0, -0.0, 300.0); } void main(int argc, char* *argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowSize(400, 300); glutInitWindowPosition(200, 100); glutCreateWindow("Mandelbort"); glClearColor(0.0, 0.0, 0.0, 0.0); glutDisplayFunc(&myDisplay); glutReshapeFunc(reshape); glutMainLoop(); }
结果如下
Julia集
在xn+1 = xn2 + z,对给定的复数z,所有使|x|不发散的x0属于Julia集
给定的z = -0.7454 + 0.1131i;x0的实部为[-1.6,1.6],虚部为[-1.2,1.2]
代码
#include <glut.h> int g_width, g_height; /********************************* 输入: z = a + bi MaxIteration,迭代次数上限 输出: 灰度级n, 0表示白色 思路: *********************************/ int GrayLevel(double x, double y, int MaxIteration) { double xx, yy, zx = -0.7454, zy = 0.1131; int n = 0; xx = x * x; yy = y * y; while(n++ < MaxIteration) { y = 2 * x * y + zy; x = xx - yy + zx; xx = x * x; yy = y * y; if ((xx + yy) > 4) { return n; } } return 0; }
结果如下: