Home > Back-end >  Random Rectangles without Overlapping some Pixels
Random Rectangles without Overlapping some Pixels

Time:01-17

I want to generate random rectangles. That is a pretty easy task. The issue is I need them to not overlap with any of these black dots:

dots
Inverted for ease of sight:

dots inverted

Now I can just tell it to ignore any rectangles it generates if it overlaps with any black dots, but as the dot density increases, it gets to enter image description here

CodePudding user response:

  1. Select a random white point.

  2. Find the nearest black dot. This will be the radius of a circle

  3. Create a circle centered at the random point.

  4. Choose 2 points on the circle. Find their opposite point that pass by the center of the circle draw the rectangle from the 4 points.

P.S. Just be sure that the 2 points are not the diameter of the circle...

  •  Tags:  
  • Related