Created
January 27, 2019 01:34
-
-
Save tarunbod/ce8739ba2fbf515b312a5055df53b931 to your computer and use it in GitHub Desktop.
mandelbrot processing sketch
This file contains 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
double xmin = -2, xmax = 2; | |
double ymin = -2, ymax = 2; | |
double cx = /*0.13499624814721625;*/ 0.25; //-1.787; // -1.785 | |
double cy = /*-0.9999288539922775;*/ 0; | |
double dx; | |
double dy; | |
int counter = 0; | |
double l = 0.1; | |
boolean reset = true; | |
boolean paused = true; | |
PFont font; | |
float roff = random(2 * PI); | |
float goff = random(2 * PI); | |
float boff = random(2 * PI); | |
void setup() { | |
size(750, 750); | |
background(0); | |
loadPixels(); | |
font = createFont("Arial", 16, true); | |
} | |
void draw() { | |
if (reset) { | |
doIt(); | |
reset = false; | |
} | |
if (!paused) { | |
doIt(); | |
} | |
} | |
void doIt() { | |
//if (counter++ % 4 == 0) { | |
dx = (xmax - xmin) / width; | |
dy = (ymax - ymin) / height; | |
double x = xmin; | |
for (int i = 0; i < width; i++) { | |
double y = ymin; | |
for (int j = 0; j < height; j++) { | |
int p = i + j * width; | |
boolean diverged = false; | |
double a = x, b = y; | |
double ta, tb; | |
int c; | |
for (c = 0; c < 100; c++) { | |
ta = a; | |
tb = b; | |
a = ta*ta - tb*tb + x; | |
b = 2*ta*tb + y; | |
if (a*a + b*b > 100000) { | |
diverged = true; | |
break; | |
} | |
} | |
if (diverged) { | |
pixels[p] = color(getColor(c, roff), getColor(c, goff), getColor(c, boff)); | |
} else { | |
pixels[p] = color(255,0,0); | |
} | |
y += dy; | |
} | |
x += dx; | |
} | |
xmin = interpolate(xmin, cx, l); | |
xmax = interpolate(xmax, cx, l); | |
ymin = interpolate(ymin, cy, l); | |
ymax = interpolate(ymax, cy, l); | |
updatePixels(); | |
textFont(font); | |
fill(0); | |
text("Mandelbrot Set", 1, 17); | |
text("R: Reset", 1, 17 * 2); | |
text("C: Colors", 1, 17 * 3); | |
text("Spacebar: Pause/Play", 1, 17 * 4); | |
text("Click: Refocus", 1, 17 * 5); | |
//} | |
} | |
void mousePressed() { | |
cx = scale(mouseX, 0.0, width, xmin, xmax); | |
cy = scale(mouseY, 0.0, height, ymin, ymax); | |
println("New Center: " + cx + ", " + cy); | |
println("window: " + (xmax - xmin) + ", " + (ymax - ymin)); | |
println(); | |
} | |
void keyPressed() { | |
if (key == ' ') { | |
paused = !paused; | |
} else if (key == 'r') { | |
xmin = -2; | |
ymin = -2; | |
xmax = 2; | |
ymax = 2; | |
reset = true; | |
paused = true; | |
} else if (key == 'c') { | |
roff = random(2 * PI); | |
goff = random(2 * PI); | |
boff = random(2 * PI); | |
doIt(); | |
} | |
} | |
double interpolate(double v0, double v1, double t) { | |
return (1 - t) * v0 + t * v1; | |
} | |
double scale(double value, double istart, double istop, double ostart, double ostop) { | |
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart)); | |
} | |
float getColor(int c, float o) { | |
return map(func(c + o), -1, 1, 0, 255); | |
} | |
float func(float x) { | |
return tan(x); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment