Created
November 25, 2016 13:23
-
-
Save bgola/5fc4b5d51bda46a17f041bc83e3f710b to your computer and use it in GitHub Desktop.
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
class Complex { | |
double real; | |
double img; | |
public Complex(double real, double img) { | |
this.real = real; | |
this.img = img; | |
} | |
public Complex multi(Complex b) { | |
double real = this.real * b.real - this.img * b.img; | |
double img = this.real * b.img + this.img * b.real; | |
return new Complex(real, img); | |
} | |
public Complex sum(Complex b) { | |
double real = this.real + b.real; | |
double img = this.img + b.img; | |
return new Complex(real, img); | |
} | |
public double distance() { | |
return sqrt((float)(this.real*this.real + this.img*this.img)); | |
} | |
} | |
int MAX_ITER = 40; | |
Complex c; | |
float rnoise = 123; | |
float inoise = 20; | |
double c_real = 0.2; | |
double c_img = 0.35; | |
int i_dir = 1; | |
int r_dir = 1; | |
void setup() { | |
size(512, 512); | |
colorMode(HSB, MAX_ITER); | |
} | |
void draw() { | |
background(0); | |
//c = new Complex(map(mouseX, 0, width, 0, 1), map(mouseY, 0, height, 0, 1)); | |
c = new Complex(c_real, c_img); | |
rnoise += 0.1; | |
inoise += 0.2; | |
c_real += map(noise(rnoise), 0, 1, 0, 0.01) * r_dir; | |
c_img += map(noise(inoise), 0, 1, 0, 0.01) * i_dir; | |
if (c_real >= 0.9 || c_real <= 0.2) { | |
r_dir = r_dir * -1; | |
} | |
if (c_img >= 0.9 || c_img <= 0.2) { | |
i_dir = i_dir * -1; | |
} | |
loadPixels(); | |
for (int i=0; i<width; i++) { | |
for (int j=0; j<height; j++) { | |
int n = cal(new Complex(map(i, 0, width, -1, 1), map(j, 0, height, -1, 1))); | |
pixels[j+i*width] = color(n, MAX_ITER/1.8, MAX_ITER/1.2); | |
} | |
} | |
updatePixels(); | |
} | |
int cal(Complex p) { | |
int n = 1; | |
Complex z = new Complex(0,0); | |
Complex p2 = p.multi(p); | |
z = p2.sum(c); | |
while (z.distance() < 2 && n < MAX_ITER) { | |
n++; | |
p2 = z.multi(z); | |
z = p2.sum(c); | |
} | |
return n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment