Mandelbrot problem
in
Programming Questions
•
1 year ago
I'm interested in fractals at the moment and I thought I had made sense of the Wikipedia article on the Mandelbrot set which defines it as z = sq(z)+c where "c" is a complex number and "z" is tested after some number of iterations to see if it tends towards infinity. I looked at Daniel Shiffman's code in the Learning section and tried to rewrite his code to be more like the above equation because that makes sense to me. Unfortunately it doesn't seem to work, it just makes a bunch of concentric circles instead and I can't seem to figure out why. The rewriting of Shiffman's code is below. Set shiffmanCode to true to use his code (which correctly makes a Mandelbrot image) or leave it false to see what I tried:
- boolean shiffmanCode = false; // Set this to true to use Shiffman's code
- float xmin = -2.5;
- float ymin = -2;
- float wh = 4;
- void setup() {
- size(400, 400, P2D);
- noLoop();
- loadPixels();
- }
- void draw() {
- int maxiterations = 200;
- float xmax = xmin+wh;
- float ymax = ymin+wh;
- float dx = (xmax-xmin)/width;
- float dy = (ymax-ymin)/height;
- float x = xmin;
- for (int i = 0; i < width; i++) {
- float y = ymin;
- for (int j = 0; j < height; j++) {
- // Shiffman variables
- float a = x;
- float b = y;
- // My variables, I am assuming Mandelbrot is z = sq(z)+c
- float z = 0;
- float cReal = x;
- float cImaginary = y;
- int n = 0;
- while (n < maxiterations) {
- // Original code from Processing's Learning section by Daniel Shiffman
- if (shiffmanCode) {
- float aa = a*a;
- float bb = b*b;
- float twoab = 2.0*a*b;
- a = aa-bb+x;
- b = twoab+y;
- if (aa+bb > 16.0) break;
- }
- // My code
- else {
- z = sq(z)+sqrt(sq(cReal)+sq(cImaginary));
- if (z > 2) break;
- }
- n++;
- }
- if (n == maxiterations) pixels[i+j*width] = 0;
- else pixels[i+j*width] = color(n*16 % 255);
- y += dy;
- }
- x += dx;
- }
- updatePixels();
- }
If anyone can either explain why his code works or why mine doesn't I'd appreciate it.
1