# Mandelbrot Set

The Mandelbrot set is a fractal. Fractals are mathematical sets that have a repeating pattern at every scale. For more information look at the bottom of this post.

The images above of the Mandelbrot set were created in processing using the pseudo code found on it's wiki. Source code can be seen below.

```boolean[] keyDown = new boolean[8];
//camera x position
float xC = -0.3850605;
//camera y position
float yC = 0.5905769;
//zoom amount (1 for 1 to 1)
float zoom = 0.14230743;
//zoom speed
float zoomSpeed = 0.01;
//movement speed
float movementSpeed = 0.1;

void setup() {
//have a small resolution if you want to be able to move around
size(960,640);
colorMode(HSB, 360, 100, 100);
}

void draw() {
//Mandelbrot set Escape time algorithm
for (int sX = 0; sX < width; sX++) {
for (int sY = 0; sY < width; sY++) {
float x0 = ((3.5 * (float)(sX) / (float)width) - 2.5) * zoom + xC;
float y0 =((2 * (float)(sY) / (float)height) - 1) * zoom + yC;
float x = 0;
float y = 0;
int iteration = 0;
int max_iteration = 280;
while (x*x + y*y < 2*2  &&  iteration < max_iteration) {
float xtemp = x*x - y*y + x0;
y = 2*x*y + y0;
x = xtemp;
iteration = iteration + 1;
}
if (iteration == max_iteration)
stroke(color(0, 100, 0));
else {
//blue and black color scheme
stroke(color(230, 100, iteration % 100));
//multi color scheme
//stroke(color(iteration % 300, 100, 100));
}
point(sX, sY);
}
}
//code for movement
movement();
}

private void movement() {
if (keyDown[0] == true) {
yC -= movementSpeed * zoom;
}
if (keyDown[1] == true) {
yC += movementSpeed * zoom;
}
if (keyDown[2] == true) {
xC -= movementSpeed * zoom;
}
if (keyDown[3] == true) {
xC += movementSpeed * zoom;
}
if (keyDown[4] == true) {
zoom -= zoomSpeed * zoom;
}
if (keyDown[5] == true) {
zoom += zoomSpeed * zoom;
}
}

public void keyPressed() {
if (key == 'w' || (key == CODED && keyCode == UP)) {
keyDown[0] = true;
}
if (key == 's' || (key == CODED && keyCode == DOWN)) {
keyDown[1] = true;
}
if (key == 'a' || (key == CODED && keyCode == LEFT)) {
keyDown[2] = true;
}
if (key == 'd' || (key == CODED && keyCode == RIGHT)) {
keyDown[3] = true;
}
if (key == '=') {
keyDown[4] = true;
}
if (key == '-') {
keyDown[5] = true;
}
}

public void keyReleased() {
if (key == 'w' || (key == CODED && keyCode == UP)) {
keyDown[0] = false;
}
if (key == 's' || (key == CODED && keyCode == DOWN)) {
keyDown[1] = false;
}
if (key == 'a' || (key == CODED && keyCode == LEFT)) {
keyDown[2] = false;
}
if (key == 'd' || (key == CODED && keyCode == RIGHT)) {
keyDown[3] = false;
}
if (key == '=') {
keyDown[4] = false;
}
if (key == '-') {
keyDown[5] = false;
}

//save current frame
if (key == 'p')
saveFrame("snapShot-####.png");
//print location and zoom
if (key == 'x')
print(xC + "," + yC + " " + zoom + "\n");
}
```