Place random non-overlapping rectangles on a panel

后端 未结 5 933
别那么骄傲
别那么骄傲 2020-12-05 11:44

I\'ve a panel of size X by Y. I want to place up to N rectangles, sized randomly, upon this panel, but I don\'t want any of them to overlap. I need to know the X, Y position

5条回答
  •  感动是毒
    2020-12-05 11:53

    I would advise you use StaxMans suggestion.

    Here is my 2c:

    Add a whole lot of rectangles randomly (overlapping each other). delete overlapping rectangles:

    for rectangle in list of rectangles:
        if rectangle not deleted:
            delete all rectangles touching rectangle.
    

    to find all the rectangles touching a particular rectangle, you can use a quad tree or inequalities based on x1,y1 x2,y2 values.

    Edit: In fact, most game engines such as pygame etc include collision detection of rectangles which is a common problem.

提交回复
热议问题