3

I have a rectangle that is W x H.

Within that rectangle is another rectangle that is rotated by ϴ degrees which is always between -45 and 45 degrees, and shares the same center as the outer rectangle. I need to find w and h such that the area of the inner rectangle is maximized.

Here's a (ghetto) image to illustrate a bit. Though, the corners of the rectangles should probably be touching, I assume?

Ghetto Image

Here is the prototype of the function I'm looking to write:

SizeD GetMaxRectangleSize(double outerWidth, double outerHeight, float angle)

SizeD is just a struct that has a width and height in doubles.


Thanks to the comments for steering me in the right direction!

My solution, though perhaps not mathematically optimal, was to assume that if all four corners of the inner rectangle fall on the outer rectangle then area will be maximized.

Therefore:

H = wSin(ϴ) + hCos(ϴ)

W = wCos(ϴ) + hSin(ϴ)

Solving for w and h and substituting gives:

h = (HCos(ϴ) - WSin(ϴ))/(cos(ϴ)^2 - sin(ϴ)^2)

w = (WCos(ϴ) - HSin(ϴ))/(cos(ϴ)^2 - sin(ϴ)^2)

Which happens to work for ϴ = [0,45), and (-45,0] should act the same.

Mills
  • 355
  • 3
  • 8
  • 1
    it looks like you're looking for the formula for w and h? Perhaps then you'll get more help from math.stackexchange.com. – redtuna Aug 27 '13 at 18:18
  • 1
    That said, they (just like us) have a high standard for questions, and you're going to have to show that you have at least tried to solve the problem by yourself before asking for help. I suggest doing that first, and then posting there. – redtuna Aug 27 '13 at 18:26
  • W=cos(theta)*w+tan(theta)*h and H=cos(theta)*h+tan(theta)*w, if you are looking for w and h just inverse the system – jambono Aug 27 '13 at 18:27
  • 1
    http://stackoverflow.com/questions/5789239/calculate-largest-rectangle-in-a-rotated-rectangle?lq=1 Related, and probably dup, but I don't know whether it is correct or not... – nhahtdh Aug 27 '13 at 18:41
  • 1
    @nhahtdh Yeah I saw that question, and perhaps dismissed it too quickly since it is looking for the max unrotated rectangle within the rotated rectangle, but it may still be usable.. I will take a second look and revise this question later tonight. – Mills Aug 27 '13 at 21:30
  • @tom10 I thanked Hans in the delegate question in the comments, he never created an answer so I never marked it. The other question didn't have a satisfactory answer, but it looks like a later edit came close so I marked that one. Anyway, thanks for pointing out that other question, I had forgotten about it. – Mills Aug 28 '13 at 18:09
  • @user1088467: I posted my answer. It basically matches your intuition for one of the cases, though a proof is useful. (Also, btw, sticking with the question/answer format of SO, it would be useful if you could post your answer as an answer to the problem.) – tom10 Aug 28 '13 at 19:04

1 Answers1

2

The tricky part of this question isn't how to calculate the area of an interior rectangle, but which of all the possible interior rectangles has maximum area?

To start with, observe that the box in your image is the same area regardless of how it is slid around horizontally, and if it is slid to the rightmost wall, it allows for an easy parameterization of the problem as follows:

I find it a bit easier to think of this problem, with the fixed box rotated by the offset angle so that the interior box lines up in a standard orientation. Here's a figure (I've changed theta to beta just because I can type it easily on a mac, and also left off the left most wall for reasons that will be clear):

enter image description here

So think of this constructed as follows: Pick a point on the right side of the exterior rectangle (shown here by a small circle), note the distance a from this point to the corner, and construct the largest possible interior with a corner at this point (by extending vertical and horizontal lines to the exterior rectangle). Clearly, then, the largest possible rectangle is one of the rectangles derived from the different values for a, and a is a good parameter for this problem.

So given that, then the area of the interior rectangle is:

A = (a * (H-a))/(cosß * sinß)

or, A = c * a * (H-a)

where I've folded the constant trig terms into the constant c. We need to maximize this, and to do that the derivative is useful:

dA/da = c * (H - 2a)

That is, starting at a=0 (ie, the circle in the figure is in the lower corner of the exterior rectangle, resulting in a tall and super skin interior rectangle), then the area of the interior rectangle increases monotonically until a=H/2, and then the area starts to decrease again.

That is, there are two cases:

1) If, as a increase from 0 to H/2, the far interior corner hits the opposite wall of the exterior, then the largest possible rectangle is when this contact occurs (and you know it's the largest due to the monotonic increase -- ie, the positive value of the derivative). This is your guess at the solution.

2) If the far corner never touches a wall, then the largest interior rectangle will be at a=H/2.

I haven't explicitly solved here for the area of the interior rectangle for each case, since that's a much easier problem than the proof, and anyone who could follow the proof, I assume could easily calculate the areas (and it does take a long time to write these things up).

tom10
  • 67,082
  • 10
  • 127
  • 137