Mandelbrot 集可视化
Mandelbrot set visualization
我正在尝试通过处理可视化 Mandelbrot 集,这是我第一次做这样的事情。我的方法很简单。
我有一个函数 Z,它实际上只是集合的主要函数 (f(z)=z^2+c
),我为屏幕的每个像素做一个循环,每次我重复使用 Z() 的过程并将结果用作函数 Z() 中的新 z 参数
不知道为什么屏幕上显示的只是一条对角线,我也不知道为什么会这样。
完整代码如下:
void draw() {
int max_iterations = 100, infinity_treshold = 16;
for (int y = 0; y < 360; y++) {
for (int x = 0; x < 480; x++) {
float z = 0; // the result of the function, (y)
float real = map(x,0,480,-2,2); // map "scales" the coordinate as if the pixel 0 was -2 and the pixel 480 was 2
float imaginary = map(y,0,360,-2,2); // same thing with the height
int func_iterations = 0; // how many times the process of the equation has been excecuted
while (func_iterations < max_iterations) {
z = Z(z, real+imaginary);
if (abs(z) > infinity_treshold) break;
func_iterations++;
}
if (func_iterations == max_iterations) rect(x,y,1,1);
}
}
noLoop();
}
private float Z(float z, float c) {
return pow(z,2)+c;
}
公式z = z^2 +c
的意思是和Complex numbers. I recommend to use PVector
一起运算来表示一个复数。例如:
private PVector Z(PVector z, PVector c) {
return new PVector(z.x * z.x - z.y * z.y + c.x, 2.0 * z.x * z.y + c.y);
}
看例子:
void setup() {
size(400, 400);
}
void draw() {
background(255);
int max_iterations = 100;
float infinity_treshold = 16.0;
for (int y = 0; y < width; y++) {
for (int x = 0; x < height; x++) {
float real = map(x, 0, width, -2.5, 1.5);
float imaginary = map(y, 0, height, -2, 2);
PVector c = new PVector(real, imaginary);
PVector z = new PVector(0, 0);
int func_iterations = 0;
while (func_iterations < max_iterations) {
z = Z(z, c);
if (z.magSq() > infinity_treshold)
break;
func_iterations++;
}
if (func_iterations == max_iterations) {
point(x, y);
}
}
}
noLoop();
}
private PVector Z(PVector z, PVector c) {
return new PVector(z.x * z.x - z.y * z.y + c.x, 2.0 * z.x * z.y + c.y);
}
您已将 z 声明为浮点数,因此它是一个实数,它应该是复数。我不熟悉处理,它甚至有复数数据类型吗?
另一个问题在Z(z, real+imaginary)
实部和虚部都是浮点数,所以是实数,所以它们的和是实数。您需要从实部和虚部构造一个复数。
我正在尝试通过处理可视化 Mandelbrot 集,这是我第一次做这样的事情。我的方法很简单。
我有一个函数 Z,它实际上只是集合的主要函数 (f(z)=z^2+c
),我为屏幕的每个像素做一个循环,每次我重复使用 Z() 的过程并将结果用作函数 Z() 中的新 z 参数
不知道为什么屏幕上显示的只是一条对角线,我也不知道为什么会这样。
完整代码如下:
void draw() {
int max_iterations = 100, infinity_treshold = 16;
for (int y = 0; y < 360; y++) {
for (int x = 0; x < 480; x++) {
float z = 0; // the result of the function, (y)
float real = map(x,0,480,-2,2); // map "scales" the coordinate as if the pixel 0 was -2 and the pixel 480 was 2
float imaginary = map(y,0,360,-2,2); // same thing with the height
int func_iterations = 0; // how many times the process of the equation has been excecuted
while (func_iterations < max_iterations) {
z = Z(z, real+imaginary);
if (abs(z) > infinity_treshold) break;
func_iterations++;
}
if (func_iterations == max_iterations) rect(x,y,1,1);
}
}
noLoop();
}
private float Z(float z, float c) {
return pow(z,2)+c;
}
公式z = z^2 +c
的意思是和Complex numbers. I recommend to use PVector
一起运算来表示一个复数。例如:
private PVector Z(PVector z, PVector c) {
return new PVector(z.x * z.x - z.y * z.y + c.x, 2.0 * z.x * z.y + c.y);
}
看例子:
void setup() {
size(400, 400);
}
void draw() {
background(255);
int max_iterations = 100;
float infinity_treshold = 16.0;
for (int y = 0; y < width; y++) {
for (int x = 0; x < height; x++) {
float real = map(x, 0, width, -2.5, 1.5);
float imaginary = map(y, 0, height, -2, 2);
PVector c = new PVector(real, imaginary);
PVector z = new PVector(0, 0);
int func_iterations = 0;
while (func_iterations < max_iterations) {
z = Z(z, c);
if (z.magSq() > infinity_treshold)
break;
func_iterations++;
}
if (func_iterations == max_iterations) {
point(x, y);
}
}
}
noLoop();
}
private PVector Z(PVector z, PVector c) {
return new PVector(z.x * z.x - z.y * z.y + c.x, 2.0 * z.x * z.y + c.y);
}
您已将 z 声明为浮点数,因此它是一个实数,它应该是复数。我不熟悉处理,它甚至有复数数据类型吗?
另一个问题在Z(z, real+imaginary)
实部和虚部都是浮点数,所以是实数,所以它们的和是实数。您需要从实部和虚部构造一个复数。