Changing Boolean to Int in a custom function

Hi! I am trying to create a bunch of random circles to appear and then bounce around the edges of the screen. I got everything I wanted so far except the “checkEdge” function. I keep getting the error that says “The operator < is undefined for the argument type(s) int[], int[]”. I was reading online that you can change a boolean result to its corresponding int value but I was unsure of how to do that. I included the full code incase you needed to see it but the part I need help on is the very last function “void checkEdge”. Any help would be greatly appreciated!

int numParticle = 25;
int[] posX = new int[numParticle];
int[] posY = new int[numParticle];
int[] radius = new int[numParticle];
int[] dirX = new int[numParticle];
int[] dirY = new int[numParticle];
color[] randomColor = new color[numParticle];

void setup() {
  size(500,500);
  colorMode(HSB);
  for (int i = 0; i < numParticle; i++) {
    posX[i] = int(random(500));
    posY[i] = int(random(500));
    radius[i] = int(random(100));
    dirX[i] = int(random(1,5));
    dirY[i] = int(random(1,5));
    randomColor[i] = color(random(255), random(255), random(255));
  }
}

void draw() {
  background(255);
  drawParticles();
  update();
  checkEdge();
}


void drawParticles() {
   noStroke();
   for(int i =0; i < numParticle; i++) {
     fill(randomColor[i]);
     ellipse(posX[i], posY[i], radius[i], radius[i]);
   }
}
void update() {
  for(int i=0; i < numParticle; i++) {
    posX[i] += dirX[i];
    posY[i] += dirY[i];
  }
}
void checkEdge() {
  if(posX > width-radius || posX < radius) dirX = -dirX;
  if(posY < radius || posY > height - radius) dirY = -dirY;
}
1 Like

There is a difference between Int and Int[].

Int > Int is how it‘s supposed to be, but what you did is Int[] > Int[], which can‘t give you a result.

You just need to look at the function above that one to see the difference in how you are refering to posX.

posX is an Int[], while posX[0] is an Int in your Code.

In that function you suddenly went from your variables being Int Arrays to refering to them as single Ints.

The Error messages tell you what you did wrong, not how it should be, since there can be different approaches that are right in some cases.

Also, i don‘t really know what you mean with that change Bool to Int… You should be able to just use a bool as Int, since they are just that… either 0, or 1.

1 Like

as @Lexyth (nice to see you here) told you, need to check the whole array

void checkEdge() {
  for (int i=0; i < numParticle; i++) {
    if ( posX[i] > width - radius[i] || posX[i] < radius[i] ) dirX[i] = -dirX[i];
    if ( posY[i] < radius[i] || posY[i] > height - radius[i]) dirY[i] = -dirY[i];
  }
}


but that is not the only problem,
https://processing.org/reference/ellipse_.html
width height means DIAMETER
and edge check need RADIUS

    ellipse(posX[i], posY[i], 2*radius[i], 2*radius[i]);


random usage:

    radius[i] = int(random(100));

would allow from 0 … 99 ( 198 diameter )
better give a low range

    radius[i] = int(random(10,50));

and if you use that on the color too,
can create/adjust a kind of color theme: “soft”-theme

    randomColor[i] = color(random(100,200), random(100,200), random(100,200));
2 Likes