Distance Checking of Static Objects

Hi. I’m trying to draw an array of 9 flowers in such a way that there is no overlap and the centres are on within the boundaries of the canvas.
I’ve come up with a method so that the flowers are at least spawning far enough away from the previous flower in the array. My only problem now is how to check the currently generating flower against all the other flowers in the array that have been generated so far. This function is essentially a pregeneration in setup() where the actual drawing is done later in draw() using buttonPressed.

float randomAngle;
float randomMultiplier;

void spawnFlowers() {
  flowers[0] = new Flower(int(random(3, 11)), width/2, height/2);
  for (int i = 1; i < flowers.length; i++) {
    randomAngle = random(0, TWO_PI);
    randomMultiplier = random(2, 4);
    float x = flowers[i-1].x + randomMultiplier*flowers[i-1].radius*cos(randomAngle);
    float y = flowers[i-1].y + randomMultiplier*flowers[i-1].radius*sin(randomAngle);
    while (x < 0 || x > width || y < 0 || y > height) { //Check if flower is within canvas boundary
      randomAngle += PI/18;
      x = flowers[i-1].x + randomMultiplier*flowers[i-1].radius*cos(randomAngle);
      y = flowers[i-1].y + randomMultiplier*flowers[i-1].radius*sin(randomAngle);
    }
    flowers[i] = new Flower(int(random(3, 11)), x, y);
    showing[i] = 0;
  }
}
1 Like

To check you need a new function and for loop over all other positions so far

Use dist() command- see reference

1 Like