How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site. Ask follow up questions if you need to.
  • Go back-and-forth until satisfied
    Rate the answer you receive.
Ask Ryan Your Own Question
Ryan, Engineer
Category: Math
Satisfied Customers: 9218
Experience:  B.S. in Civil Engineering
Type Your Math Question Here...
Ryan is online now

I am writing a computer program routine to fill a square to

This answer was rated:

I am writing a computer program routine to fill a square to a given percentage of coverage using a circle.
I need a formula that gives me the radius needed to fill the squares area by x%. Any part of the circle's area falling outside the bounds of the square isn't to be counted as filling the square's area.
I know this formula
radius = (side_length * sqrt(required_percentage)) / sqrt(pi)
gives me a circle whose area is the required percentage of the square but if doesn't take into account that some of this circle is outside the square so it is not being used to fill the square. but it works for percentages up to about 78.5% when the circle touches the edge of the square but after this some of the circle's area is not being used to fill the square.
When the circles diameter is the same length as the squares diagonal the circle fully fills the square.
Can you provide a formula that works for all percentages?


Thank you for using the site.

I can give you a formula that calculates the percentage of coverage of a circle of radius R over a square of side length s, when the value of R is greater than s/2. As you have noted, for values of R which are less than or equal to s/2, the formula that you have works fine.

The difficulty with the circle overlapping the sides of the square, however, is rearranging the formula to give you the radius, R, when you know the side length, s, and the desired coverage percentage. Because there are trigonometric and inverse trigonometric functions involved, it is not possible to solve the equation for R explicitly.

You will probably have to write a function that takes something of a brute force approach by calculating values from R = s/2 to R = (s*√2)/2 until you get the desired percentage of coverage. Of course, if the desired percentage is less than 78.5398% or so, then you can use the formula that you mentioned above.

The formula for the percentage coverage when R > s/2 is:

Percent coverage = [ (pi*R^2 - 2R^2(theta – sin theta)) / s^2 ] * 100%

where theta = 2 arccos( s / 2R)

I hope this helps.



Ryan and 2 other Math Specialists are ready to help you