# Mandelbrot Exploration

I finally figured out how to embed programs that I write onto these blog posts! I’m really excited about the way this will expand my ability to describe and explain all sorts of topics in the future. I’ll inaugurate this new ability with an old classic: a program that allows you to explore the Mandelbrot set. I’ve preset it with a pretty good resolution limit that allows for nice viewing as well as not too slow load times, but you can adjust it as you please! Enjoy!

Controls:

UP or W to zoom in
DOWN or S to zoom out
CLICK to center view on the clicked point
E to double resolution (mashing this will slow things down a LOT, so use sparingly)
Q to halve resolution
SPACE to reset everything

[pjs4wp]
double xmin = -2.25;
double xmax = .75;
double ymin = -1.1;
double ymax = 1.1;
double resolution = 200;
double pathLength = 1000;
void setup()
{
size(600,400);
background(0);
colorMode(HSB, 255, 255, 255);
drawSet();
}
void draw()
{
double cx = xmin + (xmax – xmin)*mouseX/width;
double cy = ymin + (ymax – ymin)*mouseY/height;
double x = 0;
double y = 0;
double path = 0;
stroke(255);
while (path < pathLength) { point(x,y); double yNew = 2*x*y + cy; x = x*x - y*y + cx; y = yNew; path += 1; } } void drawSet() { background(0); double xpos = 0; double ypos = 0; while(ypos < 400) { double cx = xmin + (xmax - xmin)*xpos/width; double cy = ymin + (ymax - ymin)*ypos/height; double x = 0; double y = 0; double step = 0; while(step < resolution) { double yNew = 2*x*y + cy; x = x*x - y*y + cx; y = yNew; step += 1; if(x*x + y*y > 4)
{
break;
}
}
if(4 > x*x + y*y)
{
stroke(0);
}
else
{
stroke(color(255*step/resolution,255,255));
}
point(xpos,ypos);
xpos += 1;
if (xpos == 600)
{
xpos = 0;
ypos += 1;
}
}
}
void mouseReleased()
{
double dx = xmax – xmin;
double dy = ymax – ymin;
double cx = xmin + dx*mouseX/width;
double cy = ymin + dy*mouseY/height;
xmin = cx – dx/2;
xmax = cx + dx/2;
ymin = cy – dy/2;
ymax = cy + dy/2;
drawSet();
}
void keyPressed()
{
double dx = xmax – xmin;
double dy = ymax – ymin;
if (keyCode == UP || key == ‘w’)
{
xmin += .25*dx;
xmax -= .25*dx;
ymin += .25*dy;
ymax -= .25*dy;
drawSet();
}
if (keyCode == DOWN || key == ‘s’)
{
xmin -= .5*dx;
xmax += .5*dx;
ymin -= .5*dy;
ymax += .5*dy;
drawSet();
}
if (key == ‘ ‘)
{
xmin = -2.25;
xmax = .75;
ymin = -1.1;
ymax = 1.1;
resolution = 200;
drawSet();
}
if (key == ‘e’)
{
resolution *= 2;
drawSet();
}
if (key == ‘q’)
{
resolution /= 2;
drawSet();
}
}
[/pjs4wp]

Try zooming in on a region near the edge of the set where there are black regions and increasing the resolution. “Resolution” here really means “the number of Mandelbrot iterations run to test if the point diverges or converges”, and the black points are those that were judged to be convergent. So some of these points might be found to be no longer convergent with increasing resolution! Which is why you might notice black regions suddenly filling up with color if you increase the resolution. (Of course, some regions are truly convergent, like the central chunk of the Mandelbrot set around (0,0), so such points’ colors will not depend on the resolution.)

If you’re confused about what this image is, I recommend checking out Numberphile’s video on the Mandelbrot set. (Actually, I recommend it even if you aren’t confused.)