3

I was given width and height of two rectangles and have to determine if the first one has enough space to be inside the second one.

I checked the obvious horizontal and vertical cases, but what about rotation?

Can someone give me a little hint?

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Joseph Kirtman
  • 349
  • 2
  • 10
  • Are you given an angle for each of them, or are you checking that the second one fits into the first whatever its angle? – Corentin Pane Dec 07 '19 at 14:22
  • @CorentinPane There is no information about angles. I assume the task implies that it is possible to rotate them in any manner – Joseph Kirtman Dec 07 '19 at 14:23
  • So are you looking to determine if there *exists* a rotation angle that allows one to fit in the other, or if it is possible to fit one into the other *for all* rotation angles? – Corentin Pane Dec 07 '19 at 14:25
  • @CorentinPane I'm looking to determine if the rotations make sense if neither of previous two conditions are satisfied: 1) bottom left position 2) the case when we rotate it by 90 degrees – Joseph Kirtman Dec 07 '19 at 14:31

1 Answers1

3

You can calculate dimensions of bounding box for rotated rectangle depending on rotation angle as shown here

H = w * Abs(Sin(Fi)) + h * Abs(Cos(Fi))
W = w * Abs(Cos(Fi)) + h * Abs(Sin(Fi))

where w, h are rotated rectangle dimensions and H, W are bounding box dimensions.

There might exist some intervals of angles where H<Height2 and some intervals with W<Width2. If these intervals do intersect, so rotated rectangle fits into the second one.

MBo
  • 77,366
  • 5
  • 53
  • 86