Method to count particles?

Hello Processing community,

I am new with p5.js and I am learning to do some fun simulations with it. But I would like to ask for some help.

I am trying to do a simulation about diffusion. Particles moving randomly from left to right. So far this is what I have done:

https://www.openprocessing.org/sketch/435895

I would like to know how to count the number of particles crossing the middle wall in the simulation. Or how can I count particles from one region to another?

Please, if you have any advice or a link where I can learn about this, I would appreciate it.

Thanks in advance...

Answers

  • Answer ✓

    What can you say about the horizontal position of a particle that is on the left of the center line? What value must it be less than?

    Then you just need to loop over all the particles and add one to a total for each particle that is on the left.

    The total on the right is then the total number of particles minus the number on the left.

  • Hey guys,

    I will study the methods from the link, thanks GoToLoop

    I will try your suggestions TfGuy44

    Have a good day.

  • Hello everybody,

    Thanks again for the suggestions and link. I was able to adapt some code from Jeffrey Thompson from processing to p5.js. And I was able to count the number of particles from one region to another.

    Here is what I have done:

    https://www.openprocessing.org/sketch/437388

    Other suggestions to improve the code will be appreciated.

    Have a great day. Kind regards... :)

  • Sigh.

    void setup() {
      size(600, 400);
      background(0);
      int count = 0;
      float x, y;
      for ( int i = 0; i < 500; i++) {
        x = random(width);
        y = random(height);
        if (x < width/2) { // Is it on the left half?
          count++;
          fill(255);
        } else {
          fill(128);
        }
        ellipse(x, y, 10, 10);
      }
      fill(255,0,0);
      text("Count: " + count, 20, 20);
    }
    
    void draw() {
    }
    
  • edited July 2017

    Hi TfGuy44,

    I tried to count particles using your code. And it works fine when the particles don't move around. That is when they are not being drawn constantly. If I write the code in the draw function, then the variable "count" increases constantly. It seems, I am missing something :(

    Is there a way to stop that loop in the draw function and count the particles just when they are the specific region?

    On the code I wrote, I defined another array where I store when a particle is the region or not (with 0s and 1s). Then I sum the elements of the array, so I get the number of particles.

    Here, you can see it with the counting:

    https://www.openprocessing.org/sketch/437525

    Cheers!

  • edited July 2017

    Did you make sure to set the count variable to 0 each time draw() runs, before you start counting?

  • There we go... That is what I forgot to add. ups!

    Now I will be working adding some graphs... Maybe in the future I will ask for more help...

    Thanks alot for your patience and time to have look of my code... :)

    Have a great day...

Sign In or Register to comment.