Skip to content

Instantly share code, notes, and snippets.

@racecraftr
Last active October 7, 2019 15:26
Show Gist options
  • Save racecraftr/6c77d988b27276887d24b700deb9f41a to your computer and use it in GitHub Desktop.
Save racecraftr/6c77d988b27276887d24b700deb9f41a to your computer and use it in GitHub Desktop.
Makes a colorful mandelbrot set.
// you might have to wait a while to see the mandelbrot set.
// play around with the amount of iterations to get a different result every time!
size(600, 600);// sets the size.
noLoop();
background(0);// background is black.
colorMode(HSB, 360, 100, 100);// sets the color mode to HSB values.
float w = 4;// width of the set
float h = (w*height)/width;// height of the set
// now take the negative halves of the width and height.
float xmin = (-w/2);
float ymin = (-h/2);
shows the minimum of the xy dimensions (taking place of the real and imaginary dimensions). set to -2 for each.
//float cx = xmin ;
//float cy = ymin ;
loadPixels();// loads the pixels.
long maxiter = 100L;// how many iterations it makes. Play around with this! Higher numbers take longer to finish.
//shows the maximum of the xy dimensions (taking place of the real and imaginary dimensions). set to 2 for each
float xmax = xmin + w;
float ymax = ymin + h;
// the change in x and y.
float dx = (xmax - xmin)/(width);
float dy = (ymax - ymin)/(height);
//equation: z = z^2 + c. This means that for each itration, x = the square of x + the x and the y (the real and the imaginary.
//this would work for the imaginary because i^2 = -1.)
// does each process along each of the coordinates
float y = ymin;
for(int j = 0; j < height; j++) {
float x = xmin;
for (int i = 0; i < width; i++) {
float a = x;
float b = y;
int n = 0;
while(n < maxiter) {// while it does the process less than the amount of iterations
float aa =(a * a);//z^2
float bb =(b*b);//z^2
float twoab = 2.0 * (a * b);
a = (aa - bb + x);
b = (twoab + y);
if (dist(aa, bb, 0, 0)>4.0){
break;
}
n++;
}
if (n == maxiter) {
pixels[i+j*width] = color(0);
}else{
float norm = map(n, 0, maxiter, 0, 1);
pixels[i+j * width] = color(map(sqrt(norm), 0, 1, n, 255), 100, 100);
}
x+= dx;// adds the change in x
}
y +=dy;// adds the change in y
}
// eventually makes a grid of pixels
updatePixels();// updates the look for each iteration
// full code on:
// https://processing.org/examples/mandelbrot.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment