An algorithm to space out overlapping rectangles?

后端 未结 6 1221
感情败类
感情败类 2020-11-28 18:31

This problem actually deals with roll-overs, I\'ll just generalized below as such:

I have a 2D view, and I have a number of rectangles within an area on the screen.

6条回答
  •  庸人自扰
    2020-11-28 18:50

    Here's a guess.

    Find the center C of the bounding box of your rectangles.

    For each rectangle R that overlaps another.

    1. Define a movement vector v.
    2. Find all the rectangles R' that overlap R.
    3. Add a vector to v proportional to the vector between the center of R and R'.
    4. Add a vector to v proportional to the vector between C and the center of R.
    5. Move R by v.
    6. Repeat until nothing overlaps.

    This incrementally moves the rectangles away from each other and the center of all the rectangles. This will terminate because the component of v from step 4 will eventually spread them out enough all by itself.

提交回复
热议问题