Create a gist now

Instantly share code, notes, and snippets.

@marty958 /dla.pde
Last active Mar 31, 2017

What would you like to do?
DLAprogram
ArrayList<Seed> seeds;
ArrayList<Particle> particles;
ArrayList<Integer> changes;
int i, j;
int count;
float dx;
float dy;
float dist;
float mindist;
int minnum;
float r_particle = 1.0;
float r_seed = 1.0;
float step = 1.0;
void setup(){
size(500, 500);
//initial setting
seeds = new ArrayList<Seed>();
Seed seed = new Seed();
seed.init();
seeds.add(seed);
particles = new ArrayList<Particle>();
for(j = 0; j < 10000; j++){
Particle particle = new Particle();
particle.init();
do{
particle.init();
count = 0;
dist = sqrt(pow(seed.x - particle.x, 2.0) + pow(seed.y - particle.y, 2.0));
if(dist <= (r_seed + r_particle)){
count++;
}
}while(count != 0);
particles.add(particle);
}
changes = new ArrayList<Integer>();
}
void draw(){
background(255);
changes.clear();
for(j = 0; j < particles.size(); j++){
mindist = width;
minnum = -1;
for(i = 0; i < seeds.size(); i++){
dist = sqrt(pow(seeds.get(i).x - particles.get(j).x, 2.0) + pow(seeds.get(i).y - particles.get(j).y, 2.0));
if(dist <= (r_seed + r_particle)){
minnum = i;
}
}
if(minnum != -1){
changes.add(j);
Seed snew = new Seed();
snew.x = particles.get(j).x;
snew.y = particles.get(j).y;
seeds.add(snew);
}
}
for(Integer c: changes){
do{
particles.get(c).init();
count = 0;
for(i = 0; i < seeds.size(); i++){
dist = sqrt(pow(seeds.get(i).x - particles.get(c).x, 2.0) + pow(seeds.get(i).y - particles.get(c).y, 2.0));
if(dist < (r_seed + r_particle)){
count++;
}
}
}while(count != 0);
}
for(j = 0; j < particles.size(); j++){
dx = step * ((int)random(2) * 2.0 - 1.0); //return -step or step
dy = step * ((int)random(2) * 2.0 - 1.0);
particles.get(j).x += dx;
particles.get(j).y += dy;
particles.get(j).cycleBC();
}
for(Seed s: seeds){
s.display();
}
}
class Seed{
float x;
float y;
void display(){
fill(255,0,0);
ellipse(x, y, r_seed * 2.0, r_seed * 2.0);
}
void init(){
x = width / 2;
y = height / 2;
}
}
class Particle{
float x;
float y;
void cycleBC(){
if(x < 0.0){
x = width;
}
else if(x > width){
x = 0.0;
}
if(y < 0.0){
y = height;
}
else if(y > height){
y = 0.0;
}
}
void init(){
x = random(0, width);
y = random(0, height);
}
void display(){
fill(0, 255, 0);
ellipse(x, y, r_particle * 2.0, r_particle * 2.0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment