The Mandelbrot set is a famous mathematical set of complex numbers that has captivated mathematicians,
artists, and programmers for decades.
It's a fractal, meaning that it exhibits self-similarity at different scales,
and its intricate patterns and colors make it a popular subject for generating stunning visualizations.
Recently, I ended up spending an afternoon coding an implementation in p5.js to visualize the mandelbrot set.
Who am I kidding, I also wanted to see if I could make a good looking picture like many other mandelbrot visualizations.
`setup()`
function as well as add some global variables we will use later on:let magnificationFactor = 300;
let panX = 2;
let panY = 1.5;
function setup() {
createCanvas(800, 800);
background(220);
drawHowieMandel();
}
`drawHowieMandel()`
because this was a project for fun and that name was sure to
never leave my head, feel free to change this name as you see fit. `drawHowieMandel()`
function:function drawHowieMandel(){
for(var x=0; x < 800; x++) {
for(var y=0; y < 800; y++) {
var belongsToSet =
checkMandelbrot(x/magnificationFactor - panX,y/magnificationFactor - panY);
if(belongsToSet !== 0) {
stroke((belongsToSet) * 255);
point(x,y);
}else{
//do nothing
}
}
}
}
`stroke`
function to do this in p5.js.`checkMandelbrot()`
which will take in our
desired x and y values and return if the point belongs to the Mandelbrot set.function checkMandelbrot(x,y){
var realComponentOfResult = x;
var imaginaryComponentOfResult = y;
var maxIterations = 700;
for(var i = 0; i < 700; i++) {
// Calculate the real and imaginary components of the result
// separately
var tempRealComponent = realComponentOfResult * realComponentOfResult- imaginaryComponentOfResult * imaginaryComponentOfResult+ x;
var tempImaginaryComponent = 2 * realComponentOfResult * imaginaryComponentOfResult+ y;
realComponentOfResult = tempRealComponent;
imaginaryComponentOfResult = tempImaginaryComponent;
if(realComponentOfResult * imaginaryComponentOfResult > 5){
return (i/maxIterations);
}
}
return 0;
}
`Z^2+C`
.
To calculate it, we start off with Z as 0 and we put our starting location into C.
Then you take the result of the formula and put it in as Z and the original location as C.
This is considered only one of our iterations.