How to resolve the algorithm Mandelbrot set step by step in the Processing programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Mandelbrot set step by step in the Processing programming language

Table of Contents

Problem Statement

Generate and draw the Mandelbrot set.

Note that there are many algorithms to draw Mandelbrot set and there are many functions which generate it .

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Mandelbrot set step by step in the Processing programming language

Source code in the processing programming language

double x, y, zr, zi, zr2, zi2, cr, ci, n;
double zmx1, zmx2, zmy1, zmy2, f, di, dj;
double fn1, fn2, fn3, re, gr, bl, xt, yt, i, j;
 
void setup() {
  size(500, 500);
  di = 0;
  dj = 0;
  f = 10;
  fn1 = random(20); 
  fn2 = random(20); 
  fn3 = random(20);
  zmx1 = int(width / 4);
  zmx2 = 2;
  zmy1 = int(height / 4);
  zmy2 = 2;
}
 
void draw() {
  if (i <= width) i++;
  x =  (i +  di)/ zmx1 - zmx2;
  for ( j = 0; j <= height; j++) {
    y = zmy2 - (j + dj) / zmy1;
    zr = 0;
    zi = 0;
    zr2 = 0; 
    zi2 = 0; 
    cr = x;   
    ci = y;  
    n = 1;
    while (n < 200 && (zr2 + zi2) < 4) {
      zi2 = zi * zi;
      zr2 = zr * zr;
      zi = 2 * zi * zr + ci;
      zr = zr2 - zi2 + cr;
      n++;
    }  
    re = (n * fn1) % 255;
    gr = (n * fn2) % 255;
    bl = (n * fn3) % 255;
    stroke((float)re, (float)gr, (float)bl); 
    point((float)i, (float)j);
  }
}
 
void mousePressed() {
  background(200); 
  xt = mouseX;
  yt = mouseY;
  di = di + xt - float(width / 2);
  dj = dj + yt - float(height / 2);
  zmx1 = zmx1 * f;
  zmx2 = zmx2 * (1 / f);
  zmy1 = zmy1 * f;
  zmy2 = zmy2 * (1 / f);
  di = di * f;
  dj = dj * f;
  i = 0;
  j = 0;
}

i = di = dj = 0
fn1, fn2, fn3 = random(20), random(20), random(20)
f = 10
    
def setup():
    global zmx1, zmx2, zmy1, zmy2
    size(500, 500)
    zmx1 = int(width / 4)
    zmx2 = 2
    zmy1 = int(height / 4)
    zmy2 = 2


def draw():
    global i

    if i <= width:
        i += 1
    x = float(i + di) / zmx1 - zmx2
    for j in range(height + 1):
        y = zmy2 - float(j + dj) / zmy1
        zr = zi = zr2 = zi2 = 0
        cr, ci = x, y
        n = 1
        while n < 200 and (zr2 + zi2) < 4:
            zi2 = zi * zi
            zr2 = zr * zr
            zi = 2 * zi * zr + ci
            zr = zr2 - zi2 + cr
            n += 1

        re = (n * fn1) % 255
        gr = (n * fn2) % 255
        bl = (n * fn3) % 255
        stroke(re, gr, bl)
        point(i, j)


def mousePressed():
    global zmx1, zmx2, zmy1, zmy2, di, dj
    global i, j
    background(200)
    xt, yt = mouseX, mouseY
    di = di + xt - width / 2.
    dj = dj + yt - height / 2.
    zmx1 = zmx1 * f
    zmx2 = zmx2 * (1. / f)
    zmy1 = zmy1 * f
    zmy2 = zmy2 * (1. / f)
    di, dj = di * f, dj * f
    i = j = 0

  

You may also check:How to resolve the algorithm Levenshtein distance step by step in the Processing programming language
You may also check:How to resolve the algorithm Calculating the value of e step by step in the Processing programming language
You may also check:How to resolve the algorithm Anagrams step by step in the Processing programming language
You may also check:How to resolve the algorithm Animation step by step in the Processing programming language
You may also check:How to resolve the algorithm Julia set step by step in the Processing programming language