There are two methods:

Method M1: The diameters of the semicircles have to be on the longer sides, starting at an endpoint of the rectangle. The two semicircles touch each other in the middle M of the rectangle



| | | |

b | . M | | | | |-------.---.-----------|


R should be the center of the semicircle, and because of RA = RM,

it holds that

r^2 = (a/2 - r)^2 + (b/2)^2

Solving for r gives

r = min(b,(a^2+b^2)/(4a)), where a >= b.

Method M2: We'll cut on the line y = c x, where c will turn out to be slightly less than d, the slope of the diagonal. We describe the semicircle lying above the line y = c x, having this line as the straight part of the semi-circle. The center P of the semicircle will be taken on the line y = d - x, and will be tangent to the left and top of the rectangle. Clearly the lower down P is on this line, the better. The naive solution is not optimal because the upper place where the semicircle meets the diagonal is interior to the rectangle. So we try to determine c in such a way that this latter point actually lies slightly down from the top, on the right side of the rectangle. This involves solving the quartic:

4r^4 - (4a+16b)r^3 + (16b^2+a^2+8ab)r^2 - (6b^3+4ab^2+2ba^2)r + b^4+(ab)^2 = 0,

where r < b, the details of which will be left to the reader.

The other semicircle is the reflection of the first through the origin.

After a few calculations, we find that the value of r given by M2 is greater than the one given by M1 only if 1 < a/b < 2.472434.
