Skip to content

Instantly share code, notes, and snippets.

@genmeblog
Last active September 3, 2018 20:32
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 genmeblog/ad230ae2917d4ea85235d5d916e42cf7 to your computer and use it in GitHub Desktop.
Save genmeblog/ad230ae2917d4ea85235d5d916e42cf7 to your computer and use it in GitHub Desktop.
Popcorn over vector fields
ArrayList<PVector> points = new ArrayList<PVector>();
float step = random(0.01, 0.05); // random step
Folds vector_field = new Folds(); // random vector field
float noise_scale = random(-2,2); // noise scaler
void setup() {
size(600, 600);
noStroke();
smooth(8);
fill(20, 5);
background(240);
for (int i=0; i<50000; i++) {
points.add(new PVector(random(-2, 2), random(-2, 2)));
}
}
PVector noise_field(PVector v) {
float x = v.x * noise_scale;
float y = v.y * noise_scale;
PVector field = new PVector(noise(x,y)-0.5, noise(y,x,0.3)-0.5);
field.mult(2);
return field;
}
void updateVector(PVector v) {
PVector vf = vector_field.calcField(v,1); // calc vector field
vf.add(noise_field(v)); // add vector field constructed from noise
vf.mult(TWO_PI); // multiply
v.x = v.x + step * sin(vf.x); // main formula
v.y = v.y + step * sin(vf.y);
if (abs(v.x)>4 || abs(v.y)>4) {
v.x = random(-2, 2);
v.y = random(-2, 2);
}
}
void draw() {
for (PVector v : points) {
updateVector(v);
float x = map(v.x, -3, 3, 0, width);
float y = map(v.y, -3, 3, 0, height);
rect(x, y, 1, 1);
}
}
void keyPressed() {
if(keyCode == 32) {
String name = hex((int)random(0x1000000)) + ".jpg";
save(name);
println("saved: " + name);
}
}
//
class Folds {
int fold_id;
Folds(int id) {
fold_id = id;
println(getNamebyNo(id));
precalc();
}
Folds() {
this((int)random(22)+1);
}
PVector calcField(PVector p, float weight) {
switch(fold_id) {
case 1:
return sinusoidal(p, weight);
case 2:
return butterfly(p, weight);
case 3:
return wedgejulia(p, weight);
case 4:
return rotate(p, weight);
case 5:
return cross(p, weight);
case 6:
return pdj(p, weight);
case 7:
return fan2(p, weight);
case 8:
return rings2(p, weight);
case 9:
return heart(p, weight);
case 10:
return ex(p, weight);
case 11:
return popcorn(p, weight);
case 12:
return waves(p, weight);
case 13:
return polar(p, weight);
case 14:
return horseshoe(p, weight);
case 15:
return curl(p, weight);
case 16:
return scry(p, weight);
case 17:
return rectangles(p, weight);
case 18:
return julian(p, weight);
case 19:
return juliascope(p, weight);
case 20:
return twintrian(p, weight);
case 21:
return lines(p, weight);
case 22:
return quilez(p, weight);
default:
return p;
}
}
String getNamebyNo(int no) {
switch(no) {
case 1:
return "sinusoidal";
case 2:
return "butterfly";
case 3:
return "wedgejulia";
case 4:
return "rotate";
case 5:
return "cross";
case 6:
return "pdj";
case 7:
return "fan2";
case 8:
return "rings2";
case 9:
return "heart";
case 10:
return "ex";
case 11:
return "popcorn";
case 12:
return "waves";
case 13:
return "polar";
case 14:
return "horseshoe";
case 15:
return "curl";
case 16:
return "scry";
case 17:
return "rectangles";
case 18:
return "julian";
case 19:
return "juliascope";
case 20:
return "twintrian";
case 21:
return "lines";
case 22:
return "quilez";
default:
return "none";
}
}
float t0 = random(-4, 4);
float t1 = random(-4, 4);
float t2 = random(-4, 4);
float t3 = random(-4, 4);
PVector quilez(PVector v, float weight) {
return new PVector(weight * cos(t0 + v.y + cos(t1 + PI * v.x)),
weight*cos(t2 + v.x + cos(t3 + PI * v.y)));
}
float lines_scale = random(0.03, 0.4);
boolean lines_squared = random(0, 1) < 0.5 ? true : false;
PVector lines(PVector p, float weight) {
float r;
if (lines_squared) r=0.5*sq(randomGaussian());
else r=0.25*randomGaussian();
float y = lines_scale * (floor(p.y/lines_scale) - 0.5 + r);
return new PVector(weight * p.x, weight * y);
}
float twintrian_weight = random(0.4, 1);
PVector twintrian(PVector p, float weight) {
float a = random(0, 1) * twintrian_weight * p.mag();
float sa = sin(a);
float cla = cos(a) + log(sq(sa));
if (cla<-30) cla = -30;
return new PVector(weight * 0.8 * p.x * cla, weight * 0.8 * p.x * (cla - sa * PI));
}
float julian_power = random(0, 1)<0.5? (int)random(4, 10) : -(int)random(4, 10);
float julian_dist = random(0.5, 3.0);
float julian_cpower, julian_abspower;
PVector julian(PVector p, float weight) {
float a = (atan2(p.y, p.x) + TWO_PI * floor(julian_abspower * random(0, 1)))/julian_power;
float r = weight * 2.0 * pow(sq(p.x)+sq(p.y), julian_cpower);
return new PVector(r*cos(a), r*sin(a));
}
float juliascope_power = random(0, 1)<0.5? (int)random(4, 10) : -(int)random(4, 10);
float juliascope_dist = random(0.5, 2.0);
float juliascope_cpower, juliascope_abspower;
PVector juliascope(PVector p, float weight) {
int rnd = (int)(juliascope_abspower * random(0, 1));
float a;
if (rnd % 2 == 0)
a = (2 * PI * rnd +atan2(p.y, p.x))/juliascope_power;
else
a = (2 * PI * rnd -atan2(p.y, p.x))/juliascope_power;
float r = weight * 2.0 * pow(sq(p.x)+sq(p.y), juliascope_cpower);
return new PVector(r*cos(a), r*sin(a));
}
float rectangles_x = random(0.1, 1);
float rectangles_y = random(0.1, 1);
PVector rectangles(PVector p, float weight) {
float x = weight * ((2 * floor(p.x/rectangles_x) + 1)* rectangles_x - p.x);
float y = weight * ((2 * floor(p.y/rectangles_y) + 1)* rectangles_y - p.y);
return new PVector(x, y);
}
float scry_weight = random(0.4, 1);
PVector scry(PVector p, float weight) {
float r2 = sq(p.x)*sq(p.y);
float r = 3.0 / (p.mag() * (r2 + 1.0/scry_weight));
float x = weight * r * p.x;
float y = weight * r * p.y;
return new PVector(x, y);
}
float fan2_x = random(-1, 1);
float fan2_y = random(2, 7);
float fan2_dx, fan2_dx2;
PVector fan2(PVector p, float weight) {
float r = weight * 0.8 * p.mag();
float theta = atan2(p.x, p.y);
float t = theta + fan2_y - floor((theta + fan2_y) / fan2_dx) * fan2_dx;
float ang;
if (t > fan2_dx2)
ang = theta - fan2_dx2;
else
ang = theta + fan2_dx2;
return new PVector(r * sin(ang), r * cos(ang));
}
float pdj_a = random(-3.0, 3.0);
float pdj_b = random(-3.0, 3.0);
float pdj_c = random(-3.0, 3.0);
float pdj_d = random(-3.0, 3.0);
PVector pdj(PVector p, float weight) {
return new PVector( weight * 1.5 * (sin(pdj_a * p.y) - cos(pdj_b * p.x)),
weight * 1.5 * (sin(pdj_c * p.x) - cos(pdj_d * p.y)));
}
PVector sinusoidal(PVector p, float weight) {
return new PVector(weight * 3.0 * sin(p.x), 3.0 * sin(p.y));
}
PVector butterfly(PVector p, float weight) {
float y2 = 2.0 * p.y;
float r = weight * 1.3029400317411197908970256609023 * sqrt(abs(p.y * p.x) / (1e-10 + sq(p.x) + sq(y2)));
return new PVector(r * p.x, r * y2);
}
int wedgejulia_count = (int)random(2, 7);
float wedgejulia_angle =random(-3, 3);
int wedgejulia_power = random(0, 1)<0.5?(int)random(2, 7):-(int)random(2, 7);
float wedgejulia_dist = random(1, 4);
float wedgejulia_cf, wedgejulia_cn, wedgejulia_rN;
PVector wedgejulia(PVector p, float weight) {
float r = weight * pow(sq(p.x)+sq(p.y), wedgejulia_cn);
int t_rnd = (int) ((wedgejulia_rN) * random(0, 1));
float a = (atan2(p.y, p.x) + TWO_PI * t_rnd) / wedgejulia_power;
float c = floor((wedgejulia_count * a + PI) * (1/PI) * 0.5);
a = a * wedgejulia_cf + c * wedgejulia_angle;
return new PVector(r * cos(a), r * sin(a));
}
PVector rotate(PVector p, float angle) {
float ca = cos(angle);
float sa = sin(angle);
return new PVector(ca * p.x - sa * p.y, sa * p.x + ca * p.y);
}
PVector realgaussian(PVector p, float weight) {
float a = TWO_PI * random(0, 1);
float r = weight * 3.0 * randomGaussian();
return new PVector(r*cos(a), r*sin(a));
}
PVector cross(PVector p, float weight) {
float r = sqrt(1.0 / (sq(sq(p.x)-sq(p.y)))+1e-10);
return new PVector(weight * 0.8 * p.x * r, weight * 0.8 * p.y * r);
}
float curl_c1 = random(0.1, 0.7);
float curl_c2 = random(0.1, 0.7);
PVector curl(PVector p, float weight) {
float re = 1 + curl_c1 * p.x + curl_c2 * (sq(p.x)-sq(p.y));
float im = curl_c1 * p.y + curl_c2 * 2 * p.x * p.y;
float r = weight / (re * re + im * im);
return new PVector(r * (p.x * re + p.y * im), r * (p.y * re - p.x * im));
}
float rings2_val = random(0.1, 1.2);
float rings2_val2;
PVector rings2(PVector p, float weight) {
float r = p.mag();
float theta = atan2(p.x, p.y);
float d = weight*(r - 2.0 * rings2_val2 * floor( (r+rings2_val2)/(2.0 * rings2_val2)) + r * (1.0 - rings2_val2) );
//float d = weight*(2.0 - rings2_val2 * ((int) ((r / rings2_val2 + 1) / 2) * 2 / r + 1));
return new PVector(d*sin(theta), d*cos(theta));
}
PVector heart(PVector p, float weight) {
float r = p.mag();
float theta = atan2(p.y, p.x);
float sinr = sin(r * theta);
float cosr = cos(r * theta);
return new PVector(weight * r * sinr, -r * weight * cosr);
}
PVector ex(PVector p, float weight) {
float r = p.mag();
float theta = atan2(p.x, p.y);
float xsin = sin(theta + r);
float ycos = cos(theta - r);
float x = weight * 0.7 * r * xsin * xsin * xsin;
float y = weight * 0.7 * r * ycos * ycos * ycos;
return new PVector(x+y, x-y);
}
float popcorn_c = random(-0.8, 0.8);
float popcorn_f = random(-0.8, 0.8);
PVector popcorn(PVector p, float weight) {
float x = p.x + popcorn_c * sin(tan(3.0 * p.y));
float y = p.y + popcorn_f * sin(tan(3.0 * p.x));
return new PVector(weight * 0.85 * x, weight * 0.85 * y);
}
float waves_b = random(-0.8, 0.8);
float waves_e = random(-0.8, 0.8);
float waves_c = random(-0.8, 0.8);
float waves_f = random(-0.8, 0.8);
PVector waves(PVector p, float weight) {
float x = p.x + waves_b * sin(p.y * (1.0 / (waves_c * waves_c) ));
float y = p.y + waves_e * sin(p.x * (1.0 / (waves_f * waves_f) ));
return new PVector(weight * x, weight * y);
}
PVector horseshoe(PVector p, float weight) {
float r = weight / (1.25 * (p.mag() + 1e-10));
float x = r * ((p.x - p.y) * (p.x + p.y));
float y = r * 2.0 * p.x * p.y;
return new PVector(x, y);
}
PVector polar(PVector p, float weight) {
float r = p.mag();
float theta = atan2(p.x, p.y);
float x = theta / PI;
float y = r - 2.0;
return new PVector(weight * 1.5 * x, weight * 1.5 * y);
}
void precalc() {
wedgejulia_cf = 1.0 - 0.5 / PI * wedgejulia_angle * wedgejulia_count;
wedgejulia_cn = wedgejulia_dist / wedgejulia_power / 2.0;
wedgejulia_rN = abs(wedgejulia_power);
rings2_val2 = rings2_val * rings2_val;
fan2_dx = PI * fan2_x * fan2_x + 1e-10;
fan2_dx2 = 0.5 * fan2_dx;
julian_cpower = julian_dist/julian_power/2.0;
julian_abspower = abs(julian_power);
juliascope_cpower = juliascope_dist/juliascope_power/2.0;
juliascope_abspower = abs(juliascope_power);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment