click on particle - loop over each?

Hi! I've a canvas with 400 aprox. elements (objects), each has a boolean state and i want to change that state by clicking on that object, i can achieve it by doing a dist from object x and y to mouse position. But looping over all the objects each time i click is a good choice? i think thats a perfomance flaw, i didnt try yet, but maybe in the future i want 1000 particles. The elements dont move, sorry for the english.

Thanks

Answers

  • edited April 2015 Answer ✓

    Loop over all the objects when the mouse is pressed and calculate the distances. There should be no noticeable performance issue. Here's a sketch that runs about the same amount of work each click, but for 40000 objects (each pixel).

    void setup(){
      size(200,200);
      background(0);
    }
    
    void draw(){
    }
    
    void mousePressed(){
      run_static();
    }
    
    void run_static(){
      for(int x=0; x<width;x++){
        for(int y=0;y<height;y++){
          float d = dist(mouseX,mouseY,x,y);
          stroke(random(d));
          point(x,y);
        }
      }
    }
    
  • Thanks for your help! I'll do that.

Sign In or Register to comment.