Skip to content

Instantly share code, notes, and snippets.

View nervoussystem's full-sized avatar

Jesse Louis-Rosenberg nervoussystem

View GitHub Profile
//list of current circles
ArrayList circles;
//diameter of the circle
float diameter = 10;
float diameter_sq = sq(diameter);
//where to add new circles
float max_distance = 300;
//maximum move in x or y direction at each step
float max_move = 1;
int[][] grid;
//the pixels taken up by one grid square
int grid_size = 2;
void setup() {
size(600,600);
background(255);
fill(0);
//initialize grid
grid = new int[width/grid_size][height/grid_size];
//wrap around so it cannot get too far away
//get the vector from dla_center to current_circle
PVector center_to_circle = PVector.sub(current_circle,dla_center);
float sq_dist = sq(center_to_circle.x)+sq(center_to_circle.y);
if(sq_dist > bound_radius_sq) {
float prev_dist = sqrt(sq_dist);
//make the distance outside of the radius into the amount inside
//bound_radius-(prev_dist-bound_radius)
float new_dist = 2*bound_radius-prev_dist;
center_to_circle.mult(-new_dist/prev_dist);
current_circle.add(random(-max_move, max_move),random(-max_move, max_move),0);
//add x,y force
current_circle.add(force_x,force_y,0);
for(int i=0;i<particles.size();++i) {
Particle p = (Particle) particles.get(i);
p.position = p.position+deltaT*p.velocity;
p.velocity = p.velocity+deltaT*p.force/p.mass;
}
//save previous particle forces and velocities
ArrayList prevForces = new ArrayList();
ArrayList prevVelocities = new ArrayList();
for(int i=0;i<particles.size();++i) {
Particle p = (Particle) particles.get(i);
prevVelocities.add(p.velocity);
prevForces.add(p.force);
}
//compute estimate
for(int i=0;i<particles.size();++i) {
PVector attractionForce = computeAttraction();
//rotate 90 degrees
float spiralForceX = attractionForce.y;
float spiralForceY = -attractionForce.x;
//vary noise discretely to approximate gradient
float xGradient = (noise(x+DELTA,y)-noise(x-DELTA,y))/(2*DELTA);
float yGradient = (noise(x,y+DELTA)-noise(x,y-DELTA))/(2*DELTA);
particle.position().add(noiseStrength*xGradient, noiseStrength*yGradient,0);
for(int i=0;i<F.length;++i) {
for(int j=0;j<F[0].length;++j) {
F_next[i][j] = F[i][j]+deltaT*diffusionRate/deltaXSq*(F[(i+1)%F.length][j]+
F[(i-1+F.length)%F.length][j]+
F[i][(j+1)%F[0].length]+
F[i][(j-1+F[0].length)%F[0].length]-
4*F[i][j]);
}
}
float FNext[][] = new float[F.length][F[0].length];
float r = deltaT*diffusionRate/deltaXSq;
for(int i=0;i<F.length;++i) {
for(int j=0;j<F[0].length;++j) {
//get the neighboring values with boundary conditions
float iPrev, iNext, jNext, jPrev;
if(i==0) iPrev = F[i][j];
else iPrev = F[i-1][j];
if(i==F.length-1) iNext = F[i][j];
else iNext = F[i+1][j];