三、曼德勃罗集合(Mandelbrot Set)
曼德勃罗集合(Mandelbrot Set)或曼德勃罗复数集合,是一种在复平面上组成分形的点的集合,因由曼德勃罗提出而得名。曼德博集合可以使复二次多项式
进行迭代来获得。其中,c是一个复参数。对于每一个c,从 z = 0 开始对fc(z)进行迭代。序列
的值或者延伸到无限大,或者只停留在有限半径的圆盘内(这与不同的参数c有关)。曼德布洛特集合就是使以上序列不延伸至无限大的所有c点的集合。
最后,我们给出一个利用C语言生成Mandelbrot集合并绘制图形的程序(该程序来自文献【1】):
#include
#include
#include
#define width_size 800
#define height_size 600
#define Maxval 255
static const float orig_x = width_size * 2/3;
static const float orig_y = height_size * 1/2;
static const pixel dim_gray = { 105, 105, 105 };
typedef struct _pixel {
unsigned char r;
unsigned char g;
unsigned char b;
} pixel;static unsigned char iteration(int x, int y)
{
const int limit = Maxval + 1;
int i;
complex c = ((x - orig_x) / (width_size / 3)) +
((orig_y - y) / (height_size / 2)) * I;
complex z = 0;
for (i = 0; i < limit; i++) {
/* basic formula */
z = z * z + c;
if (creal(z) > 2 || cimag(z) > 2)
break;
}
return (unsigned char) (i == limit ? 0 : i);
}
int main()
{
FILE *f = fopen("mandelbrot.ppm", "w+");
/* PPM header */
fprintf(f,
"P6\n" /* PPM magic number */
"#Mandelbrot Set\n"
"%d " /* width, in ASCII decimal */
"%d\n" /* height, in ASCII decimal */
"%d\n", /* maximum color value, in ASCII decimal */
width_size, height_size, Maxval);
/* Write every pixel generated by Mandelbrot Set */
for (int i = 0; i < height_size; i++) {
for (int j = 0; j < width_size; j++) {
unsigned char iter = iteration(j, i);
if (iter) {
pixel p = {
.r = iter,
.g = (float) abs(j - orig_x) / width_size * Maxval,
.b = (float) abs(i - orig_y) / height_size * Maxval };
fwrite(&p, sizeof(pixel), 1, f);
} else {
fwrite(&dim_gray, sizeof(pixel), 1, f);
}
}
}
fclose(f);
return 0;
}
上述程序所生成的图像结果如下图所示,需要补充说明的是:该图像文件格式为ppm,在Windows下你可以使用Photoshop来查看这种类型的图像文件,在OS X系统下你可以使用免费的GIMP软件来查看它。
标签:unsigned,int,曼德勃罗,height,图案,width,分形,pixel,size
来源: https://www.cnblogs.com/seeby/p/13754372.html