Exploring Julia Sets

Here’s a natural follow-up to my last post on the Mandelbrot set – an interactive Julia set explorer!

The Julia set corresponding to a particular point c = x + iy in the complex plane is defined as the set of complex numbers z that stay finite upon arbitrary iterations of the following function: fc(z) = z2 + c. The Mandelbrot set, by comparison, is defined as the set of complex numbers c such that the value obtained by starting with 0 and iterating the function fc arbitrarily many times converges.

What’s remarkable is now beautiful and complex the patterns that arise from this simple equation are. Take a look for yourself: just hover over a point to see its corresponding Julia set!

[pjs4wp]
float xmin = -1.8;
float xmax = 1.8;
float ymin = -1.2;
float ymax = 1.2;
float resolution = 30;
void setup()
{
size(600,400);
background(0);
stroke(255);
}
void draw()
{
background(0);
float cx = xmin + (xmax-xmin)*(float)(mouseX)/(float)(width);
float cy = ymin + (ymax-ymin)*(float)(mouseY)/(float)(height);
drawJuliaSet(cx, cy);
stroke(255);
fill(255);
line(-xmin*width/(xmax-xmin),0,-xmin*width/(xmax-xmin),height);
line(0,-ymin*height/(ymax-ymin),width,-ymin*height/(ymax-ymin));
}
void keyPressed()
{
if (key == ‘e’) resolution += 10;
if (key == ‘q’) resolution -= 10;
if (key == ‘ ‘) resolution = 30;
}
void drawJuliaSet(float cx, float cy)
{
text(“(” + (str)((int)(cx*100)/100.) + “,” + (str)(-(int)(cy*100)/100.) + “)”, 20, 20);
float i = 0;
float j = 0;
while (height > j)
{
x = xmin + (xmax-xmin)*(float)i/width;
y = ymin + (ymax-ymin)*(float)j/height;
float step = 0;
while (step < resolution) { float xNew = x*x - y*y + cx; y = 2*x*y + cy; x = xNew; if (x*x + y*y > 4) break;
step += 1;
}
stroke(color(0,255,0));
fill(color(0,255,0));
if (4 > x*x + y*y) point(i,j);
i += 1;
if (i == width)
{
i = 0;
j += 1;
}
}
stroke(0);
fill(color(255,0,0));
ellipse((cx-xmin)*width/(xmax-xmin),(cy-ymin)*height/(ymax-ymin),10,10);
}
[/pjs4wp]
Resolution is preset at a value good for seeing lots of details and loading at a reasonable speed, but should you want to change it, controls are ‘E’ to increase it and ‘Q’ to decrease it. To reset to default, press ‘SPACE’.

Leave a Reply