Detect if one rect can be put into another rect

后端 未结 4 1628
春和景丽
春和景丽 2021-02-13 06:39

This problem is different from testing if one rect is in another rect.

Known information is the sides length of two rects.

How to calculate if one rect can be pu

4条回答
  •  悲哀的现实
    2021-02-13 07:30

    This is a great question! If and only if one of these conditions is satisfied does a smaller rectangle with sides p and q (p >= q) fit completely into a larger rectangle with sides a and b (a >= b):

    enter image description here

    or

    enter image description here

    See this for reference.


    So if we had variables a, b, p, q, we could check if such a rectangle arrangement would be possible by evaluating:

    (p <= a && q <= b) || (p > a &&
                            b >= (2*p*q*a + (p*p-q*q)*sqrt(p*p+q*q-a*a)) / (p*p+q*q))
    

    EDIT: Thanks to @amulware for posting this alternate version in his comment:

    enter image description here

提交回复
热议问题