pixel collision

Hi , i have an image that have points that was created from an array
with x/ y pixels positions, each point represent a sound, when i touch
each point with the mouse it read that sound.

Im doing this iterating over over all the elements of my array and
comparing it with the x/y position of the mouse.

Is there is a better way in terms of speed and cpu consumtion of
calculating pixel collision with mouse ?

If i use agents instead of the mouse , each agent is gonna iterate over all the array each frame, so if i have 2000 x/y positions in my array it would iterate 4000 times per agent each frame and this is very slow, i just can run a few agents. any idea of how to solve this?

any advice ?

thanks

I’m not sure if I’m getting it right: you have a x*y array containing sounds and if you click on x,y you want to play the corresponding sound, right?

if this is the case, you should simply do something like:

  
  
...  
#define MIN_X  //  
#define MAX_X //limits for your sensible area  
#define MIN_Y  //  
#define MAX_Y //  
  
...  
int width = MAX_X-MIN_X;  
int height= MAX_Y-MIN_Y;  
int n = width*height; //sounds number  
...  
  
ofSoundPlayer sounds[n]; //or any other sound class you'll want to use  
...  
void testApp::mousePressed(int x, int y, int button){  
   if(x>MIN_X && x<MAX_X && y>MIN_Y && y<MAX_Y){  
      int s = (y-MIN_Y)*width+(x-MIN_X); //your sound index  
      sounds[s].play();  
   }  
}