random placement of rectangles with no overlaps

后端 未结 3 1801
臣服心动
臣服心动 2021-01-03 11:53

I am looking for a sound algorithm that would randomly place a given number of rectangles of the same size into a bigger rectangle (canvas).

I see two ways to do it:

相关标签:
3条回答
  • 2021-01-03 12:14

    Here is a simple heuristic. It will be non-overlapping and random.

    Place a rectangle randomly. Then, calculate the intersections of extensions of the the two parallel edges of the first rectangle with the edges of the canvas. You will obtain four convex empty regions. Place other rectangles in these empty regions one-by-one independently and calculate the similar divisions for placements. And try to put the remaining rectangles in empty regions.

    You can try different strategies. You can try to place the rectangles close to the corners. Or, you can place them around the center of the regions. We cannot discuss optimality because you introduced randomness.

    0 讨论(0)
  • 2021-01-03 12:20

    I create internal room-like dungeons using the following method.

    1) Scatter N points at random, but not within a few pixels of each other. 2) For each point in turn, expand if possible in all four directions. Cease expanding if you hit another rectangle. 3) Cease the algorithm when no rooms can expand.

    The result is N rectancles with just a few rectangular small spaces.

    Code is in the binary image library

    https://github.com/MalcolmMcLean/binaryimagelibrary/blob/master/dungeongenerator3.c #

    0 讨论(0)
  • 2021-01-03 12:36

    You might find Quadtrees or R-trees useful for your purpose.

    0 讨论(0)
提交回复
热议问题