Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if one rect can be put into another rect

Tags:

math

rect

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 put into another rect?

can rect a be put into rect b

like image 646
cuixiping Avatar asked Dec 09 '12 03:12

cuixiping


1 Answers

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

like image 105
arshajii Avatar answered Oct 12 '22 21:10

arshajii