Distance between particles always ends up with zero?

Hi,

I am playing with particle system and am trying to create a group of particles within which each particle is connected to the particle that's closest in distance with a line. However, when I use the for loop and the dist() function to find the shortest distance, the returned result is for some reason always zero.

Could someone help me understand why this is happening? I have pasted my code below.

Thank you for your time in advance.

-Frank


ArrayList particles = new ArrayList();
int closest_i = 0;
float d = 400;

void setup() {
  size(400, 200);
  background(50);
  smooth();

  fill(255);
  textSize(90);
  text("NEON", 30, 130);

  for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
      if (get(x, y) == color(255)) {
        // number to control the number of particles generated
        if(random(1) < 0.05) {
          particles.add(new Particle(x, y));
        }
      }
    }
  }
}

void draw() {
  background(50);

  for (int i = 0; i < particles.size(); i++) {
    Particle p1 = (Particle) particles.get(i);
    p1.display();
    p1.update();

    for(int j = 0; j < particles.size(); j++) {
      if(i != j) {
        Particle p2 = (Particle) particles.get(j);
        float dt = dist(p1.x, p1.y, p2.x, p2.y);
        if(dt < d) {
          d = dt;
          closest_i = j;
          println("d: " + d);
          println("closest index: " + closest_i);
          println("-------------------------");
        }
      }
    }

    //println("index: " + i + ", " + "closest: " + closest_i);
    Particle pc = (Particle) particles.get(closest_i);
    stroke(28, 152, 232, 20);
    line(p1.x, p1.y, pc.x, pc.y);
  }
}

class Particle {
  float x, y;

  Particle(float x0, float y0) {
    x = x0;
    y = y0;
  }

  void display() {
    stroke(28, 152, 232);
    point(x, y);
  }

  void update() {
    x += floor(random(-1, 2));
    y += floor(random(-1, 2));
  }
}
Tagged:

Answers

  • Answer ✓

    i think the problem is that you're only using one 'closest' value, not one for each particle. at the very least i think you need to reset closest_i and d outside the j loop.

  • Answer ✓

    @koogs is right:

    for (int i = 0; i < particles.size(); i++) {
        Particle p1 = (Particle) particles.get(i);
        p1.display();
        p1.update();
    
        int closest_i = 0;
        float d = 400;
    
        for (int j = 0; j < particles.size(); j++) {
          if (i != j) {
            Particle p2 = (Particle) particles.get(j);
            float dt = dist(p1.x, p1.y, p2.x, p2.y);
            if (dt < d) {
              d = dt;
              closest_i = j;
              //println("d: " + d);
              //println("closest index: " + closest_i);
              //println("-------------------------");
            }
          }
        }
    
  • Thank you GoToLoop, koogs, and asimes for your help! After moving the resetting statements within the loop, it works now. I just need to figure out the performance based on the examples GoToLoop shared.

Sign In or Register to comment.