Skip to content

Instantly share code, notes, and snippets.

@PatrickMurphy
Created August 1, 2017 22:47
Show Gist options
  • Save PatrickMurphy/6d47a31053891845c9d6635c4ea2f4dd to your computer and use it in GitHub Desktop.
Save PatrickMurphy/6d47a31053891845c9d6635c4ea2f4dd to your computer and use it in GitHub Desktop.
class FieldVector {
PVector location;
PVector grid_location;
PVector heading;
FieldVector(PVector loc, PVector grid_loc) {
this(loc, grid_loc, PVector.random2D());
}
FieldVector(PVector loc, PVector grid_loc, PVector heading) {
this.location = loc;
this.grid_location = grid_loc;
this.heading = heading.setMag(1);
this.heading.add(PVector.fromAngle(random(-5,5)));
heading.setMag(1);
}
void display() {
stroke(33);
PVector location2 = location.copy().add(heading);
line(location.x, location.y, location2.x, location2.y);
}
void update() {
if(random(1000)>990){
// this.heading.add(PVector.fromAngle(random(-2,2)));
heading.setMag(1);
}
}
}
class FlowPoint{
int id;
PVector location;
PVector velocity;
int frames;
color hue;
FlowPoint(){
this.location = new PVector(random(width),random(height));
this.velocity = new PVector();
this.hue = colors[floor(random(0,150))%colors.length];
this.id = start_id++;
this.frames = floor(random(6*frameRate));
}
void display(){
//fill(mona.get(floor(location.x),floor(location.y)));
// noStroke();
//ellipse(location.x,location.y,1,1);
}
void update(){
this.velocity.limit(1);
int i = max(0,min(floor(location.x / scale),(width/scale)-1));
int j = max(0,min(floor(location.y / scale),(height/scale)-1));
this.velocity.add(field[i][j].heading);
this.location.add(velocity);
if(!isInBounds() || frames++ > 6*frameRate)
reset();
}
void reset(){
this.location = new PVector(random(width),random(height));
this.velocity = new PVector(0,0);
this.hue = colors[floor(random(0,150))%colors.length];
this.frames = floor(random(6*frameRate));
}
boolean isInBounds() {
return (this.location.x >= 0 && this.location.x <= width) && (this.location.y >= 0 && this.location.y <= height);
}
ArrayList<FlowPoint> getNearPoints(){
ArrayList<FlowPoint> tmp = new ArrayList<FlowPoint>();
for(FlowPoint p : points){
FlowPoint other = p;
if(other.id != this.id){ // don't draw a line to itself
if(dist(other.location.x,other.location.y,this.location.x,this.location.y) < distTol_2 && this.id > other.id){
tmp.add(other);
}
}
}
return tmp;
}
}
FieldVector[][] field;
color[] colors = new color[]{color(228,221,81),color(110,166,213),color(255)};
ArrayList<FlowPoint> points;
int scale = 10;
float noiseScale = 0.002;
int start_id = 0;
int count = 700;
int distTol_1 = 10;
int distTol_2 = 12;
PImage mona;
void setup(){
size(1280,720);
frameRate(30);
mona = loadImage("http://wallpaperswide.com/download/the_starry_night-wallpaper-1280x720.jpg", "jpg");
mona.loadPixels();
points = new ArrayList<FlowPoint>();
for(int i = 0; i < count; i++)
points.add(new FlowPoint());
setupField();
background(15,15,15);
}
void draw(){
stroke(255);
fill(255);
update();
if(millis() % 10000 == 0)
setupField();
}
void update(){
for(FlowPoint p : points){
p.update();
ArrayList<FlowPoint> otherPoints = p.getNearPoints();
if(otherPoints.size() > 0)
for(FlowPoint other : otherPoints){
float currDist = dist(other.location.x,other.location.y,p.location.x,p.location.y);
if(currDist<=distTol_1){
stroke(lerpColor(mona.get(floor(other.location.x),floor(other.location.y)),mona.get(floor(p.location.x),floor(p.location.y)),map(currDist,0,distTol_1,0,1))); // change color based on how close it is to other point
}else{
stroke(lerpColor(mona.get(floor(p.location.x),floor(p.location.y)),mona.get(floor(other.location.x),floor(other.location.y)),map(currDist,distTol_1,distTol_2,0,1))); // fade color to bg as it gets further away
}
line(other.location.x,other.location.y, p.location.x,p.location.y);
}
}
}
void setupField(){
println("reset");
noiseSeed((long)random(1002010010));
//background(15);
field = new FieldVector[width/scale][height/scale];
for(int i = 0; i < width/scale; i++){
for(int j = 0; j < height/scale; j++){
float noi = noise(i*noiseScale, j*noiseScale)*360;
field[i][j] = new FieldVector(new PVector(i*scale,j*scale),new PVector(i,j),PVector.fromAngle(noi));
}
}
}
void mousePressed(){
setupField();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment