[Math] the ratio of a circle’s diameter to a rectangle’s width so that the circle covers the rectangle exactly

circleseuclidean-geometrygeometryratiorectangles

Ratio example image

I am doing some web design, and I need a way to dynamically figure out what to set a circle's diameter to so that it exactly covers a rectangle, as shown in the above image. In the first scenario (in the image), I calculated that the diameter of the circle was 12% greater than the width of the rectangle, and thought this would work for all rectangles, but it does not. The problem is that the width of the rectangle is dynamic, so I never know what it's value will be ahead of time, I have to size the circle based off of whatever dimensions the rectangle is at the time.

This is probably a fairly simple formula, but I am far from being a mathematician, clearly. If I can do anything to clarify my question, please ask! Thanks!

Best Answer

The diameter of the circle is equal to the diagonal of the rectangle. Let $w$ and $h$ be, respectively, the width and height of the rectangle. Then the diameter of the circle $d$ satisfies

$$d^2=w^2+h^2\iff d=\sqrt{w^2+h^2}.$$

Or, letting $a$ denote the ratio of the rectangle's height to its width, i.e. $a=\frac{h}{w}$, we have:

$$d^2=(1+a^2)w^2 \iff d=w\sqrt{1+a^2}.$$

Related Question