Created
October 18, 2010 17:30
-
-
Save hktechn0/632632 to your computer and use it in GitHub Desktop.
Mandelbrot Set
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 width = 640; | |
int height = 480; | |
float[][] c = new float[width][height]; | |
double aspect = (double)height / (double)width; | |
int offsetx = width / 2; | |
int offsety = height / 2; | |
double da = pow(2.0f, -7); | |
int calc_cnt = 20; | |
int mx, my; | |
boolean is_drag = false; | |
boolean show_grid = false; | |
void setup() | |
{ | |
size(width, height); | |
noStroke(); | |
colorMode(HSB, 1.0f); | |
mandelbrot(0, 0, width, height); | |
} | |
void draw() | |
{ | |
background(0); | |
for(int x = 0; x < width; x++) { | |
for(int y = 0; y < height; y++) { | |
if(c[x][y] >= 0.0) { | |
stroke(c[x][y], 1.0, 1.0); | |
point(x, y); | |
} | |
} | |
} | |
if(show_grid) { | |
stroke(100, 100, 100); | |
line(0, height / 2, width, height / 2); | |
line(width / 2, 0, width / 2, height); | |
} | |
} | |
void mousePressed() | |
{ | |
mx = mouseX; | |
my = mouseY; | |
} | |
void mouseDragged() | |
{ | |
is_drag = true; | |
} | |
void mouseReleased() | |
{ | |
if(!is_drag) return; | |
is_drag = false; | |
int diffx = mouseX - mx; | |
int diffy = mouseY - my; | |
move_graph(diffx, diffy); | |
} | |
void keyPressed() | |
{ | |
if('1' <= char(key) && char(key) <= '9') { | |
zoom_graph(char(key) - '0'); | |
} | |
else if(key == '+') { | |
calc_cnt *= 2; | |
println("divergent times: " + calc_cnt); | |
mandelbrot(0, 0, width, height); | |
} | |
else if(key == '-') { | |
calc_cnt /= 2; | |
println("divergent times: " + calc_cnt); | |
mandelbrot(0, 0, width, height); | |
} | |
else if(key == ' ') { | |
show_grid = !show_grid; | |
} | |
} | |
void move_graph(int dx, int dy) | |
{ | |
int lastx = width - abs(dx); | |
int lasty = height - abs(dy); | |
int destx, srcx, desty, srcy; | |
println("move: " + dx + ", " + dy); | |
for(int x = 0; x < lastx; x++) { | |
if(dx > 0) { | |
destx = width - x - 1; | |
srcx = lastx - x - 1; | |
} | |
else { | |
destx = x; | |
srcx = -dx + x; | |
} | |
for(int y = 0; y < lasty; y++) { | |
if(dy > 0) { | |
desty = height - y - 1; | |
srcy = lasty - y - 1; | |
} | |
else { | |
desty = y; | |
srcy = -dy + y; | |
} | |
c[destx][desty] = c[srcx][srcy]; | |
} | |
} | |
offsetx += dx; | |
offsety += dy; | |
if(dx > 0) { | |
mandelbrot(0, 0, dx, height); | |
if(dy > 0) { mandelbrot(dx, 0, width, dy); } | |
else { mandelbrot(dx, -dy, width, height); } | |
} | |
else { | |
mandelbrot(-dx, 0, width, height); | |
if(dy > 0) { mandelbrot(0, 0, -dx, dy); } | |
else { mandelbrot(0, -dy, -dx, height); } | |
} | |
println("done."); | |
} | |
void zoom_graph(int x) | |
{ | |
int ofx, ofy; | |
double new_da; | |
println("zoom: x" + x); | |
ofx = offsetx - width / 2; | |
ofy = offsety - height / 2; | |
new_da = da / x; | |
offsetx = (int)((double)ofx * da / new_da) + width / 2; | |
offsety = (int)((double)ofy * da / new_da) + height / 2; | |
println("scale: 1px => " + new_da); | |
da = new_da; | |
mandelbrot(0, 0, width, height); | |
} | |
void mandelbrot(int startx, int starty, int endx, int endy) | |
{ | |
print("caluculating..."); | |
for(int x = startx; x < endx; x++) { | |
double a = da * (x - offsetx); | |
for(int y = starty; y < endy; y++) { | |
c[x][y] = is_mandelbrot(a, da * (y - offsety)); | |
} | |
} | |
println(" done."); | |
} | |
float is_mandelbrot(double a, double b) | |
{ | |
double x, y, nx; | |
int i; | |
x = 0.0; | |
y = 0.0; | |
for(i = 0; i < calc_cnt; i++) { | |
nx = x * x - y * y ; | |
y = 2.0 * x * y; | |
if(nx > 3.5 || y > 3.5) { | |
return sin((float)i / (float)calc_cnt); | |
} | |
x = nx + a; | |
y += b; | |
} | |
return -1.0f; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment