# Finding the sides of a rectangle inscribed in a right triangle given an aspect ratio

Everything I have found on google/se has been about finding a maximum area rectangle, regardless of aspect ratio.

I am trying to inscribe a rectangle inside of a right triangle. I do not know the width or height of the rectangle, but I know the aspect ratio.

My hunch is that I need to find where w^2 + h^2 of my rectangle match the distance from the corner to the hypotenuse of my bounding triangle. Unfortunately I have forgotten a majority of the geometry I took in high school and can't figure out how I would do this, and my efforts to find a solution on Google have yielded mostly just maximum-area solutions without the fixed aspect-ratio constraint I have.

I have posted a related question on SO that goes in to more detail of what I am trying to do: https://stackoverflow.com/questions/33509077/dynamic-resize-image-to-different-sizes-with-paperclip-based-on-aspect-ratio

• The rectangle has sides parallel to the two legs? – lulu Nov 3 '15 at 21:33
• Yep, the rectangle and triangle will share a corner and two sides (up to the height/width of the inner rectangle). – Mike Manfrin Nov 3 '15 at 21:34
• And by "aspect ratio" you just mean that length/width is some prescribed number? – lulu Nov 3 '15 at 21:35
• Yep, I have a starting width/height, but I want to resize the rectangle (image) to be as large as it can within the triangle, preserving the ratio of w:h. – Mike Manfrin Nov 3 '15 at 21:36
• What are your inputs? Triangle sides? Triangle side and an angle? (Equivalent, but it might help get a more efficient answer.) – turkeyhundt Nov 3 '15 at 21:36

Let the aspect ration be $m$. Put the triangle in the coordinate plane with the right angle at the origin and the legs along the coordinate axes. Take the line $y=mx$ and extend it until it hits the hypotenuse. That gives you a diagonal for your rectangle.

Note: if, instead of an explicit aspect ratio you just have a rectangle in the right ratio (but the wrong scale) then place your rectangle in the same coordinate plane, with one corner at the origin and sides aligned with the coordinate axes. Then the diagonal for that rectangle is the line you want.

• How do I know when it hits the hypotenuse? – Mike Manfrin Nov 3 '15 at 21:41
• Well...what data do you have? If the other triangle vertices are $(a,0)$,$(0,b)$ then the hypotenuse is the line $y=-\frac ba\,x+b$. So the x-coordinate of the intersection is $x=\frac b{m+\frac ba}$. – lulu Nov 3 '15 at 21:43

Let's say the vertices of the right triangle are at the origin, on the $x$-axis at $x=a$, and on the $y$ axis at $y=b$. The hypotenuse has the equation $y = -\frac{b}{a}x + b.$ This describes a right triangle with sides $a,b,\sqrt{a^2+b^2}$ in the first quadrant.

Then, one diagonal of your inscribed rectangle will be $y = mx$.

Edit: To clarify, $m$ is the slope of the line that defines what will be the diagonal of the rectangle. If $0<m<1$ the rectangle is wider than it is tall; if $m=1$ it's a square; if $m>1$ it's taller than it is wide. The aspect ratio of the right triangle, similarly, is $b/a$. If $a=b$ it's an isosceles right triangle.

You can solve these two equations for $x$ and $y$, which will be the two sides of your inscribed rectangle in terms of $a,b,m$:

$$x = \frac{ba}{ma+b};$$ $$y = \frac{mba}{ma+b}.$$

Depending on which side you chose to be $a$ and which you chose to be $b$, this may or may not give the larger area, so try both (or see if you can understand which one will give you the bigger value).

• How is the aspect ratio of the rectangle finding its way in here? This seems it would return the largest area of any rectangle in the triangle, regardless of the scale? Apologies, I haven't worked with geometry in many years. – Mike Manfrin Nov 3 '15 at 23:55
• Edited my answer to explain more. – John Nov 4 '15 at 17:42