[Math] Finding formula for an arc of a circle that fills a rectangle

circlesgeometry

I'm working on a program where I need to draw an arc in a rectangle fulfills these requirements:

1) The arc must be part of a perfect circle (not the band!), must not be oval
2) The arc intersects the rectangle at the bottom left and bottom right corners
3) The height of the arc does not exceed the height of the rectangle
4) If the arc were assumed to be part of a circle, what the circle's center point would be

The requirements are such as many drawing libraries use a center point to calculate how to draw the arc accurately. The only information that we can use as input is the width and height of the rectangle, and the required output would be the center point of the circle and the radius.

The image below shows an example – the input box, and the output circle.

image

I could write a brute force method to solve this, but I'd like something more elegant. I have found many sites which have answers like this that use a parabolic formula with width and height, but in this case the height (of the arc) is not definite- if, for example, the rectangle were very tall, the arc would only use space at the bottom of the rect.

If the rectangle were perfectly square, or the height of the rect was larger than the width, the formula would be easy-

(using a coordinate system where 0,0 is the bottom left corner)
circle.centerPoint = (rect.width / 2, 0);
circle.radius = (rect.width / 2);

But as the rect gets longer, the center point needs to drop and the radius needs to get larger.
I feel that this could be really easily figure out, but I just don't have the mathematical background to handle it. Is there a formula out there to solve this?

Disclaimer: I am bad at math, and never took a calculus class. Please keep answers simple.

Best Answer

In addition to the answer pointed out by @n_b I would like to show how to reach there:

The final result:

$$\LARGE\boxed{R=\frac H2+\frac{W^2}{8H}}$$

float cos_t = (R-H)/R;
P_x=(rect.width/2);
P_y=-R.cos_t;

Process:

enter image description here

Without using calculus(actually it would never be solved with calculus) and only using trigonometry:

The component of R along $\theta$ is $R.\cos\theta$ and perpendicular to it is $R.\sin\theta$

Now remaining component is $R-Rcos\theta=R(1-\cos\theta)$

Now we have: $$ W=2.R.\sin\theta\implies\sin\theta=\frac W{2R}\\ H=R(1-\cos\theta)\implies\cos\theta=1-\frac HR=\frac{R-H}R $$ Using the identity $\sin^2\theta+\cos^2\theta=1$ $$\left(\frac W{2R}\right)^2+\left(\frac{R-H}R\right)^2=1$$ $$\frac{W^2}{4R^2}+\frac{R^2+H^2-2RH}{R^2}=1\\\text{ I assume you are familiar with }(a\pm b)^2=a^2+b^2\pm 2ab$$ $$\frac{W^2}4+R^2+H^2-2RH=R^2$$ $$\frac{W^2}4+H^2=2RH$$ $$R=\frac{W^2}{8H}+\frac H2$$ It is easy to see that center is at the midpoint of width. Also y-coordinate is $-R.\cos\theta$

Related Question