Skip to content

Instantly share code, notes, and snippets.

@mharju
Created December 18, 2012 17:17
Show Gist options
  • Save mharju/4329895 to your computer and use it in GitHub Desktop.
Save mharju/4329895 to your computer and use it in GitHub Desktop.
Ghastly images in Processing
void setup()
{
size(1000, 500);
verts = new ArrayList<Vertex>();
for(int i=0;i<50;i++) {
verts.add(new Vertex(random(75, 150)));
}
background(bg);
}
class Vertex {
public PVector x;
public PVector v;
public color c;
public float r;
public Vertex(float r) {
this.r = r;
this.x = new PVector(random(0, width), random(0, height));
this.v = new PVector(random(-1, 1), random(-1, 1));
this.c = color(random(255), random(128, 255), 0, 128);
}
public void draw() {
if(showParticles) {
stroke(this.c);
fill(this.c);
ellipse(x.x, x.y, this.r, this.r);
}
}
public void update() {
if(x.x < 0 || x.x > width) { v.x = -v.x; }
if(x.y < 0 || x.y > height) { v.y = -v.y; }
x.x += v.x;
x.y += v.y;
}
public void lineTo(Vertex o) {
line(x.x, x.y, o.x.x, o.x.y);
}
public double dist(Vertex o) {
/*return pow(
pow(x.x - o.x.x, lPMetric) +
pow(x.y - o.x.y, lPMetric), invLP);*/
return x.dist(o.x);
}
public List<Vertex> neighbors(ArrayList<Vertex> verts)
{
ArrayList<Vertex> opts = new ArrayList<Vertex>();
for(Vertex v : verts) {
if(this != v && v.dist(this) < v.r) {
opts.add(v);
}
}
return opts;
}
public void affect(Vertex o) {
PVector d = new PVector(o.x.x, o.x.y);
d.sub(this.x);
float m = d.mag();
d.normalize();
o.v.x += d.x * 50.0 / (m * m);
o.v.y += d.y * 50.0 / (m * m);
o.v.x = constrain(o.v.x, -5.0, 5.0);
o.v.y = constrain(o.v.y, -5.0, 5.0);
}
}
ArrayList<Vertex> verts;
boolean isRunning=true;
boolean showParticles=false;
float lPMetric = 2, invLP = 1.0f / 2.0f;
color bg = color(0);
void keyPressed() {
if(key == 'h') {
isRunning = !isRunning;
} else if(key == 'r') {
background(bg);
} else if(key == 'p') {
showParticles = !showParticles;
background(bg);
} else if(key >= '0' && key <= '9') {
lPMetric = (float)(key - '0');
invLP = 1.0f / lPMetric;
println("Metric: "+lPMetric);
background(bg);
}
}
void draw()
{
if(isRunning) {
if(showParticles) {
background(bg);
}
for(int i=0;i<verts.size();i++)
{
Vertex v = verts.get(i);
v.draw();
for(Vertex n : v.neighbors(verts)) {
if(!showParticles) { stroke(random(240,255), 0, 128+128*sin(radians(frameCount * 0.2)), random(3,5)); }
else { stroke(255, 0, 0); }
v.lineTo(n);
n.affect(v);
}
v.update();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment