Mandelbrot set visualization

Chappie733 :

I'm trying to visualize Mandelbrot's set with processing, and it's the first time I do something like this. My approach is pretty simple. I have a function Z, which is literally just the set's main function (f(z)=z^2+c) and i do a loop for each pixel of the screen, every time i repeat the process of using Z() and using the result as the new z parameter in the function Z() For some reason what shows up on the screen is only a diagonal line, and i have no idea of why that is.

Here's the full code:

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;
}
Joni :

You've declared z as float so it's a real number, it should be complex. I'm not familiar with processing, does it even have a complex number data type?

Another problem is at Z(z, real+imaginary) Real and imaginary are both floats, so real numbers, so their sum is a real number. You need to construct a complex number from the real and imaginary parts.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=350272&siteId=1