# Area of Circle Overlapped by Rectangle

I'm trying to determine 'how much' (as a percentage) a 2D rectangle fills a 2D circle.

Actual Application:

I was comparing the accuracy of some computer game weapons by calculating the max possible dispersion from the weapon's shell origion at a given range. After that, I added a player stand in to help visualize the possible dispersion vs size of the target. Of course I can eyeball the values, but I don't know how to calculate this geometry (as the player's head and feet would not actually be inside the dispersion area, so a basic area calculation is not accurate).

Any guidance is appreciated.

UPDATE:

I'm sorry that the question was not more clear, I'll try to elaborate:

In the case of the smaller circle, if you did a basic area calculation of the circle (1.828) and the rectangle (1.6), the result would say that the rectangle fills ~87% of the circle. However, the person cannot be compacted, and their upper body and lower body do not fall within the circle, and therefore the result is not accurate.

Now I think all I need to do is to subtract the difference of the circle's diameter from the max extents of the rectangle (so 2m - 1.526 = 0.474, or in other words, just make the rectangle as tall as the circle's diameter) making the rectangle's new area 1.526 * 0.8. Making the new percentage ~69%, which should be much more accurate. Am I on the right track?

Image:

Actual Values from the Test:

Player: 2m tall, 0.8m wide.

Weapon Dispersion Circle A (green): radius = 0.763.

Weapon Dispersion Circle B (red): radius = 1.05.

• If the rectangle falls entirely inside the circle, then the answer may be found by taking a ratio of the rectangle's area to the circle's area. Othewise some specifics of how the rectangle overlaps the circle are needed. Jun 5, 2015 at 3:11
• It is one the right track, the correct ratio should be around $\frac{1.1623128}{\pi (0.763)^2} \approx 63.55\%$. However, instead of setting the height of the rectangle as the diameter $d = 1.526$ of circle, I will set it to a number between $d$ and the side length of the intersection $\ell = 2 \sqrt{0.763^2 - 0.4^2} \approx 1.29949$. Let's say we approximate the height of the rectangle as $\frac{2d+\ell}{3} \approx 1.45050$, the ratio becomes $\frac{1.45050\times 0.8}{\pi (0.763)^2} \approx 63.45\%$. Accurate to within $1%$ of the correct value. Jun 5, 2015 at 12:14
• @achille hui: Very interesting. If you don't mind, I do have some followup questions: 1) Why did you decide to use (2d+L)/3 to find the ratio? 2) You mentioned the correct ratio 'should be around a number' and that your estimate was close to the "correct value". So how did you determine what the correct value is in the first place? Did you use a more sophisticated series of steps? At any rate, thank you for the help, I really appreciate it.
– Joe
Jun 5, 2015 at 19:07
• 1) When the width of the rectangle is not too big, one can approximate the top/bottom of the circle by a quadratic polynomial, If you compute the average height of the circle within the rectangle, you will obtain the expression $(2d+L)/3$ as the width tends to $0$. 2) The correct value is computed by the function given in my answer. It covers all the possible ways ( there are tons of them) a circle can intersect with a rectangle and give you the actual area (up to machine accuracy). Jun 5, 2015 at 19:27
• @achille hui: I see, thank you very much for the clarification!
– Joe
Jun 6, 2015 at 5:01

The problem is symmetric when the shapes are centered to each other. As such, I can see 4 cases of overlap between a circle of radius $$R$$ and a rectangle of half lengths $$a$$ and $$b$$.

The shaded area corresponding to one quadrant only is calculated as follows: Consider the cases where $$b \leq a$$ and vary the radius $$R$$ from small to large.

$$\text{(Area)} = \begin{cases} \tfrac{\pi}{4} R^2 & R \leq b \\ \tfrac{1}{2} b \sqrt{R^2-b^2} + \tfrac{1}{2} R^2 \sin^{-1}\left( \tfrac{b}{R} \right) & b < R \leq a \\ \tfrac{1}{2} \left( a \sqrt{R^2-a^2} + b \sqrt{R^2-b^2} \right) + \tfrac{1}{4} R^2 \left( 2 \sin^{-1}\left( \tfrac{a}{R} \right) + 2 \sin^{-1} \left( \tfrac{b}{R} \right)- \pi \right) & a < R \leq \sqrt{a^2+b^2} \\ a b & R > \sqrt{a^2+b^2} \end{cases}$$

You can check that at the transition values of $$R$$ the area results match between adjacent cases. For example area (2) when $$R=b$$ is $$\tfrac{1}{2} b^2 \sin^{-1} \left( \tfrac{b}{b} \right) + \tfrac{1}{2} b \sqrt{b^2-b^2} = \tfrac{\pi}{4} b^2$$ which equals the area (1) when $$R=b$$.

This is not an answer but a long comment to present some code to compute the numbers.

The numerical values of the two areas are

1.162312801155704 // ar_area(0.763,0,0,-0.4,-1,0.4,1)
// ~ 72.64455% of rectangle, 63.55125% of circle

1.59553612458975  // ar_area(1.050,0,0,-0.4,-1,0.4,1)
// ~ 99.72101% of rectangle, 46.06575% of circle


Computing using an user-defined function ar_area(r,xc,yc,x0,y0,x1,y1). The r is the radius of the circle, (xc,yc) its center. The (x0,y0) and (x1,y1) specify the lower-left and upper-right corner of the rectangles.

Following are the actual code in maxima I used to compute these numbers. I think it should be obvious how to translate it to other languages.

ar_h(u) := if( u >= 1 ) then
%pi
else if( u > -1 ) then
%pi - acos(u) + u*sqrt(1-u^2)
else
0;

if( u^2 + v^2 <= 1) then
(ar_h(u)+ar_h(v))/2 - %pi/4 + u*v
else if( u <= -1 or v <= -1) then
0
else if( u >= 1 and v >= 1 ) then
%pi
else if( u >= 1 )then
ar_h(v)
else if( v >= 1 ) then
ar_h(u)
else if( u >= 0 and v >= 0 ) then
ar_h(u)+ar_h(v) - %pi
else if( u >= 0 and v <= 0 ) then
ar_h(v)
else if( u <= 0 and v >= 0 ) then
ar_h(u)
else
0;