Assume it's given a set of rectangles with different areas and some rectangles could overlap. Objective is generating of a uniform random point among rectangles' areas.
Rectangle is defined as a pair of two points:
- (x1,y1) - bottom leftmost corner;
- (x2,y2) - top rightmost corner.
My strategy for uniform distribution of a random point among not overlapped rectangles is that,- randomly select a rectangle based on areas (existing solution):
for(int i = 0; i < rectangles.length; i++) {
int area = (rectangles[i].x2 - rectangles[i].x1) *
(rectangles[i].y1 - rectangles[i].y2);
if(rand.nextInt(total + area) >= total) {
selected = i;
break;
}
total += area;
}
Then generate an arbitrary point within a rectangle:
- x1 +(1/(x2-x1))*rand(0,(x2-x1-1)),
- y1 +(1/(y2-y1))*rand(0,(y2-y1-1)).
But how to be if some of rectangles could overlap?