I thought I'd post this mandelbrot implementation, nothing original, but still pretty awesome
Left click: zoom in
Right click: zoom out
Up: increase iterations
Down: decrease iterations
Code:
int maxIteration = 32;
float xLeft = -2;
float xRight = 2;
float yTop = -2;
float yBottom = 2;
boolean update = true;
void setup()
{
size(800, 600);
mapColor(4);
}
void draw()
{
if(update)
{
for(int i = 0; i < width; i++)
{
for(int j = 0; j < height; j++)
{
int iteration = 0;
float x = 0;
float y = 0;
float x0 = map(i, 0, width, xLeft, xRight);
float y0 = map(j, 0, height, yTop, yBottom);
while(x*x + y*y < 4 && iteration < maxIteration)
{
float xtemp = x*x - y*y + x0;
y = 2*x*y + y0;
x = xtemp;
iteration++;
}
if(iteration == maxIteration)
{
stroke(0);
}
else
{
stroke(mapColor(iteration, maxIteration));
}
point(i, j);
}
}
update = false;
}
}
void mousePressed()
{
float w = (xRight - xLeft);
float h = (yBottom - yTop);
float xnew = map(mouseX, 0, width, xLeft, xRight);
float ynew = map(mouseY, 0, height, yTop, yBottom);
float newW = w/2;
float newH = h/2;
if(mouseButton == RIGHT)
{
newW = 2*w;
newH = 2*h;
}
xLeft = xnew-newW/2;
xRight = xnew+newW/2;
yTop = ynew-newW/2;
yBottom = ynew+newW/2;
update = true;
}
void keyPressed()
{
if(keyCode == UP)
{
maxIteration *= 2;
}
else if(keyCode == DOWN)
{
maxIteration /= 2;
}
maxIteration = max(maxIteration, 32);//At least 32 iterations
update = true;
}
color mapColor(int index)
{
return mapColor(index, 64);
}
color mapColor(int index, int nColors)
{
int mod = ceil(pow(nColors, 0.33));
mod = max(mod, 4);//At least 64 colors
return mapColorInternal3(index%mod * 255/(mod-1), (index/mod)%mod* 255/mod, (index/(mod*mod))%(mod-1) * 255/(mod-1));
}
color mapColorInternal1(int c1, int c2, int c3)
{
return color(c1, c2, c3);
}
color mapColorInternal2(int c1, int c2, int c3)
{
return color(c2, c1, c3);
}
color mapColorInternal3(int c1, int c2, int c3)
{
return color(c3, c1, c2);
}