Last active
December 29, 2015 22:18
-
-
Save siddMahen/7735288 to your computer and use it in GitHub Desktop.
Mandelbrot fractals in Processing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int maxiter = 500; | |
| //float zoom = 0.005; | |
| //float fx = -0.745; | |
| //float fy = 0.1; | |
| float zoom = 4; | |
| float fx = 0; | |
| float fy = 0; | |
| color[] colour = new color[maxiter]; | |
| void setup(){ | |
| size(700,700); | |
| color[] bases = new color[7]; | |
| bases[0] = color(173,0,76); //red | |
| bases[1] = color(255,100,0); //orange | |
| bases[2] = color(255,225,0); //yellow | |
| bases[3] = color(0,51,102); //purple | |
| bases[4] = color(0,215,0); //green | |
| bases[5] = color(20,0,160); //blue | |
| bases[6] = color(255,51,153); //pink | |
| // setup the color gradient | |
| int k = 0; | |
| for(int n = 0; n < maxiter; n+=maxiter/10){ | |
| color a = bases[(k % 7)]; | |
| color b = bases[(k+1) % 7]; | |
| for(int m = 0; m < maxiter/10; m++){ | |
| colour[n + m] = lerpColor(b,a,1/log(m + 1)); | |
| } | |
| k++; | |
| } | |
| // 0 is always black | |
| colour[0] = color(0,0,0); | |
| drawMandelbrot(); | |
| } | |
| void drawMandelbrot(){ | |
| loadPixels(); | |
| for(int i = 0; i < width; i++){ | |
| for(int j = 0; j < height; j++){ | |
| int c = mandelbrotColour(scaleCoords(i,j)); | |
| pixels[(j*width)+i] = colour[c]; | |
| } | |
| } | |
| updatePixels(); | |
| } | |
| void draw(){} | |
| void mouseClicked(){ | |
| PVector vec = scaleCoords(mouseX,mouseY); | |
| fx = vec.x; | |
| fy = vec.y; | |
| zoom = zoom/4; | |
| println("focus: ("+nfc(vec.x,9)+","+nfc(vec.y,9)+")"); | |
| println("zoom: "+zoom); | |
| drawMandelbrot(); | |
| } | |
| PVector f(PVector z, PVector c){ | |
| float a = pow(z.x,2) - pow(z.y,2) + c.x; | |
| float b = 2*z.x*z.y + c.y; | |
| return new PVector(a, b); | |
| } | |
| int mandelbrotColour(PVector c){ | |
| PVector curr = new PVector(0,0); | |
| for(int k = 0; k < maxiter; k++){ | |
| curr = f(curr, c); | |
| if(curr.mag() >= 2){ | |
| return k; | |
| } | |
| } | |
| return 0; | |
| } | |
| PVector scaleCoords(float i, float j){ | |
| float x = zoom*(i/width - 1/2.0) + fx; | |
| float y = zoom*(1/2.0 - j/height) + fy; | |
| return new PVector(x,y); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment