# Finding formula for an arc of a circle that fills a rectangle

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.

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);

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?

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:

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$

One of my colleagues found me the answer pretty quickly, which I must say makes me rather ashamed.

R = H/2 + (W*W / 8H)

So if W = 18 and H = 6,

R = 6/2 + (18 * 18 / 8 * 6)
R = 3 + (324 / 48)
R = 3 + 6.75
R = 9.75

finding the centerpoint is then easy enough ( don't know why I listed it as a required output, but still):

centerpoint = (W / 2), (H - R)
centerpoint = (18 / 2), (6 - 9.75)
centerpoint = 9, -3.75

Given the width $w$ and height $h$ of a rectangle, let's find the circle, in terms of its position $P$ and radius $r$, that fits your description and has its top at the very top of the rectangle (first assuming $h \leq w$).

Let $A$ be the bottom right point of the rectangle. Let $B$ be the point in the middle top of the rectangle. There is an isosceles triangle $PAB$ with sides of length $PA = r$, $PB=r$, and $AB=\sqrt{h^2+\frac{w^2}{4}}$. Further, the two equal angles are given by atan$(\frac{w}{2h})$. This is enough to give you $r$, then $P$ is $r$ below $B$.

Note that if $h > w$, then you have to return the result as if $h = w$.