minimizing overlap in random rectangles

前端 未结 5 1831
时光取名叫无心
时光取名叫无心 2021-02-09 03:14

I have a number of possibly overlapping rectangles, of random size and position within a fixed plane. Since these rectangles are random, some may not overlap:

|-----
         


        
5条回答
  •  没有蜡笔的小新
    2021-02-09 03:38

    Are the rectangles parallel to the x&y axes? I suppose they are.

    You can try using KD-Trees.

    Or if you want something homegrown and not necessarily efficient you could 'rectangulate' and then if needed merge rectangles back.

    By 'rectangulation' I mean you first find a bigger rectangle in which all rectangles fit (basically the rectangle formed by least left edge, greated right edge, least bottom edge, greatest top edge).

    Now extend out all the edges of the rectangles to cut across the big rectangle. You now have a 'rectangulation'. Basically all this means is that you sort the vertical edges and the horizontal edges and pick adjacent pairs to form a small rectangle. For each small rectangle, you can now check if this is part of the interesting area or not and reject it if it is not (It is either full within or fully outside).

    Now you have a list of small rectangles (possibly O(n^2), in your case perhaps ~2500) which make up the area of your interest. If the number is sufficiently small enough for your future processing, you could just use these, or you could merge them together to reduce the number of rectangles.

    To merge you could consider a rectangle and consider 4 possiblitlies for a merge (adjacent rectangle of same height to right or left, or adjacent rectangle of same width to top or bottom).

    You could speed up some processing (not just during merge) by maintaining a sorted list of edges (both horizontal and parallel) and maybe some hashtables.

提交回复
热议问题