How do I make a random array interactive?

I want to make random numbers in an array interactive.

int[] numbers = new int[500];
void setup() {
  size(500, 500);
  for (int i = 0; i< 450; i++)
  {
    numbers[i] = (int)random(0, 450);
  }
}

void draw() {
  background(255);
  circle(numbers[0], 50, 30);
  circle(numbers[1], 50, 30);
  if (mousePressed == true && dist(mouseX, 50, numbers[0], 50) > 50) {
    println("test");
  }
}

I have this so far, and it’s working for just one circle. But this should work for e.g. 20 circles, I do not want to repeat it over and over again, instead use a few lines of code.

1 Like

Use

void mousePressed(){

}

And put the code inside it.

Or you could have a int named toggle which increments (there are other ways to do it too)

Ie

int toggle =0;

void mousePressed(){
  toggle ++;
  if(toggle ==2){
    toggle =0;
  }
}

Then you could put your code to run in the draw loop if toggle == 1.

1 Like

I’m not sure what you mean. I must have worded my question wrongly. I meant that I wanted to calculate if the mouse is on a circle, so if I draw twenty circles (for example), I would like to have code to track a mouse pressed on each of those circles.

Then make the circles an object and have a function that checks the distance of the mouse to the center of the circle, return true if dist is less than radius.

Then have a boolean or toggle variable for the object.
Again you can use a global boolean variable or global toggle, and create another function in your object, which detects if the global mouse boolean/toggle is true/1 if it is and the mouse is within radius, then run some code

Unfortunately, I’m not allowed to use objects/classes because I’m making a school assignment. Would you happen to know any other method to check this? Thanks for your help so far.

If you’re using a pvector just put your code in an array which itterates through the pvector array.

If the code works only for one item it means you’re only checking one item, use another for loop to iterate through all of them and check mouse position for all your circles.

1 Like

That worked wonders! Thank you so much! My only problem is now how I would remove the circle after it has been clicked

So you could iterate down youre array instead of up, and if you’re condition is true, just use the array remove function.

You have to iterate down as it will cause problems with the index if you remove an array item, ie, if you are on array index 10 and the mouse pos is true, you therefore remove said index, well now index 10 has been replaced by index 11 and when you use i++ to iterate you for loop will now be checking you previous index 12 and will have missed index 11.

Alternatively you could just use i-- just if the condition is true, it should in principle solve the issue, but ive always seen a full backwards iteration recommended.

My program freezes if I’m doing i-- :frowning:. Any suggestions or an example?

Do

for(int i=array.size()-1;i>-1;i--){

}

Put your code inside that for loop and this way it shouldnt cause any issues when removing an item.

If I try to compile that, this error shows up: Cannot invoke size() on the array type int[].
Code:

int[] xnums = new int[500];
int[] ynums = new int[500];
void setup() {
  size(500, 500);
  for (int i = 0; i< 500; i++)
  {
    xnums[i] = (int)random(0, 450);
  }
  for (int i = 0; i< 450; i++)
  {
    ynums[i] = (int)random(0, 450);
  }
}

void draw() {
  background(255);
  for (int i=0; i < 20; i++) {    
    circle(xnums[i], ynums[i], 30);
  }
  for (int i=xnums.size()-1; i>-1; i-–) {
  for (int i=0; i < 20; i++) {
    if (mousePressed == true && dist(mouseX, 50, xnums[i], 50) < 5) {
      println("test");
    }
  }
}
}

1 Like

Sounds like you’re using a plain array and not an arraylist in which case you should use array.length

3 Likes