Skip to content

Instantly share code, notes, and snippets.

@bgola
Created December 5, 2016 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgola/56ef175829fb2682515a9c587f13133d to your computer and use it in GitHub Desktop.
Save bgola/56ef175829fb2682515a9c587f13133d to your computer and use it in GitHub Desktop.
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 = 70;
Complex c;
float rnoise = 123;
float inoise = 20;
double c_real = 0.2;
double c_img = 0.6;
int i_dir = 1;
int r_dir = 1;
void setup() {
size(820, 820);
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.8 || c_real <= 0.3) {
r_dir = r_dir * -1;
}
if (c_img >= 0.8 || c_img <= 0.3) {
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();
//rectMode(CENTER);
//fill(0);
//rect(map((float)c_real, 0.2, 0.9, 0, width), map((float)c_img, 0.2, 0.9, 0, height), 5, 5);
}
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;
}
void keyPressed() {
if (key == 's') {
saveFrame("/tmp/frame.jpg");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment