概述

mandelbrot

julia

Mandelbrot

对全体复数z,满足x =  x + z从x = 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 = , y = , xx = , yy = ;
int n = ;
while(n++ < MaxIteration)
{
y = * x * y + b;
x = xx - yy + a;
xx = x * x;
yy = y * y;
if ((xx + yy) > )
{
return n;
}
}
return ;
} void myDisplay()
{
double min_a, max_a, min_b, max_b, step_a, step_b, a, b;
int MaxIteration = ;
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 = ; x < g_width; x++)
{
b = min_b;
for (y = ; y < g_height; y++)
{
Iteration = - 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(, (height - width / w_aspect) / ,
width, width / w_aspect);
} else
glViewport((width - height * w_aspect) / , ,
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(, ); glutInitWindowPosition(, ); glutCreateWindow("Mandelbort"); glClearColor(0.0, 0.0, 0.0, 0.0); glutDisplayFunc(&myDisplay); glutReshapeFunc(reshape); glutMainLoop();
}

结果如下

Mandelbrot和Julia-LMLPHP

Julia集

在x =  x + z,对给定的复数z,所有使|x|不发散的x属于Julia集

给定的z = -0.7454 + 0.1131i;x的实部为[-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 = ;
xx = x * x;
yy = y * y;
while(n++ < MaxIteration)
{
y = * x * y + zy;
x = xx - yy + zx;
xx = x * x;
yy = y * y;
if ((xx + yy) > )
{
return n;
}
}
return ;
}

结果如下:

Mandelbrot和Julia-LMLPHP

04-13 22:29