Random function and constrain

You can either test a new ball if it’s not inside the rect and only then display it


void setup () {
  size (1000, 1000);
  background (255);

  rectMode (CENTER);
  fill (255, 0, 0, 40);
  rect (width/2, height/2, 300, 300);
}

void draw() {
  float x = random (width);
  float y = random (height);

  if ( ! (x>width/2-150 && x<width/2+150)) {  // **and the y-position of the ball???? How do you have to add this?   The ! means logical NOT** 
    fill(0, 30);
    ellipse (x, y, 30, 30);
  }
}

OR you just make the random values in a way that it’s automatically outside the inner rectangle, which is a bit harder, since the allowed area is AROUND the forbidden inner rectangle… (especially to distribute the points evenly)

Chrisir

1 Like