Minimal Symmetric Difference of Concentric Disk and Square

Logos, icons, avatars and similar graphics are often displayed in disk or square shaped frames. To design graphics that work well with both frame shapes, I wanted to know the relative size of a disk and a square with common center and minimal symmetric difference.

More formally, given the unit disk ${D}_{1}\left(O\right)$, centered on the origin $O$, and a square $S\left(a,O\right)$, with side length $a$ and centered on the origin, determine ${a}_{\mathrm{min}}$, such that the symmetric difference of disk and square is minimal; as a formula: ${a}_{\mathrm{min}}=\underset{a}{\mathrm{min}}\left[{D}_{1}\left(O\right)∆S\left(a,O\right)\right]$.

Domain

Figure 1 shows two cases for $a=\sqrt{2}$ and $a=2$.

If $a$ is less than $\sqrt{2}$ (the square is a proper subset of the disk), the symmetric difference of disk and square is larger than for $a=\sqrt{2}$.

Similary, if $a$ is greater than $2$ (that means the disk is a proper subset of the square), the symmetric difference of disk and square is larger than for $a=2$.

So, overall $\sqrt{2}\le {a}_{\mathrm{min}}\le 2$.

Parts of Set Difference

If $\sqrt{2}<{a}_{\mathrm{min}}<2$, as exemplified in figure 2, then the symmetric difference consists of four corners of the square and four segments of the disk. So, we need to determine the area of a corner, ${A}_{◸}\left(a\right)$, and area of a segment, ${A}_{⌓}\left(a\right)$, as functions of $a$ and minimize their sum.

We will almost do that. Instead of functions of $a$, we will determine the areas as functions of an angle with a one-to-one relationship to $a$ for the values we are interested in, minimize their sum, and then convert back to $a$.

Segment

Figure 3 illustrates the case of the segment. We call the half angle of the segment $\alpha$. Since the radius of our disk is $1$,

$a=2cos\left(\alpha \right)$

holds. We will determine the area of the segment (and later that of the corner) as functions of the angle $\alpha$.

Again using the fact that the radius of our disk is $1$, we get the following simple formula for the area of the segement (see for example the Wikipedia article on circular segments):

${A}_{⌓}\left(\alpha \right)=\alpha -\frac{1}{2}sin\left(2\alpha \right)$.

Corner

Next we determine the area of the corner. The case is illustrated in figure 4. What we called a corner so far, is formally a isosceles right triangle minus a segment. So the area of a corner, ${A}_{◸}\left(\alpha \right)$, is the area of a triangle, ${B}_{◸}\left(\alpha \right)$, minus the area of a segment, ${B}_{⌓}\left(\alpha \right)$, or

${A}_{◸}\left(\alpha \right)={B}_{◸}\left(\alpha \right)-{B}_{⌓}\left(\alpha \right)$.

We call the half angle of the segment $\beta$ and half of the segement's corresponding secant $b$.

Then, the area of the triangle is ${b}^{2}$, which is ${\mathrm{sin}}^{2}\left(\beta \right)$.

Next, we apply three equalities to transform the formula further. First, applying ${sin}^{2}\left(x\right)=\frac{1}{2}\left(1-cos\left(2x\right)\right)$ gives us $\frac{1}{2}\left(1-cos\left(2\beta \right)\right)$. Then, we use $\beta =\frac{\pi }{4}-\alpha$ to get $\frac{1}{2}\left(1-cos\left(\frac{\pi }{2}-2\alpha \right)\right)$. Finally, $cos\left(\frac{\pi }{2}-x\right)=sin\left(x\right)$ gives us

${B}_{◸}\left(\alpha \right)=\frac{1}{2}-\frac{1}{2}sin\left(2\alpha \right)$.

Now, for the segment we can use the same area formula as we did for ${A}_{⌓}\left(\alpha \right)$, since it is a segment of our unit disk, which has radius $1$. Thus the area of the segment is $\beta -\frac{1}{2}sin\left(2\beta \right)$. We apply two transformations. First, we us $\beta =\frac{\pi }{4}-\alpha$ to get $-\alpha -\frac{1}{2}sin\left(\frac{\pi }{2}-2\alpha \right)+\frac{\pi }{4}$. And second, we apply the equation $sin\left(\frac{\pi }{2}-x\right)=cos\left(x\right)$ to get

${B}_{⌓}\left(\alpha \right)=-\alpha -\frac{1}{2}cos\left(2\alpha \right)+\frac{\pi }{4}$.

So overall, the we have the following formula area of a corner as a function of $\alpha$:

${A}_{◸}\left(\alpha \right)={B}_{◸}\left(\alpha \right)-{B}_{⌓}\left(\alpha \right)=\alpha -\frac{1}{2}sin\left(2\alpha \right)+\frac{1}{2}cos\left(2\alpha \right)-\frac{\pi }{4}+\frac{1}{2}$.

Optimization Problem

In the previous sections, we determined the area of one corner and one segment. The symmetric difference consists of four corners and four segments, hence the area of the symmetric difference as a function of $\alpha$ is given by

$A\left(\alpha \right)=4\left({A}_{◸}\left(\alpha \right)+{A}_{⌓}\left(\alpha \right)\right)=8\alpha -4sin\left(2\alpha \right)+2cos\left(2\alpha \right)-\pi +2$.

Before we find the minimum, we have a look at the domain of $A\left(\alpha \right)$. We already determined, that the minimum must be assumed for $a$ between $\sqrt{2}$ and $2$. We also know that $\alpha =acos\left(\frac{a}{2}\right)$, a bijection for the values of $a$ that we are interested in. So $A\left(\alpha \right)$ should assume the minimum for $\alpha$ between $0$ and $\frac{\pi }{4}$.

Now, to find the minimum, we write down the derivative of $A\left(\alpha \right)$:

$A\text{'}\left(\alpha \right)=8-8cos\left(2\alpha \right)-4sin\left(2\alpha \right)$.

From this we get the following equation for the derivative's roots:

$2=2cos\left(2\alpha \right)+sin\left(2\alpha \right)$.

We apply two equalities to transform that equation. First we use $cos\left(2x\right)=1-{sin}^{2}\left(x\right)$, which gives us $0=-2{sin}^{2}\left(\alpha \right)+sin\left(2\alpha \right)$. Second we use $sin\left(2x\right)=2sin\left(x\right)cos\left(x\right)$, which gives us $0=-2{sin}^{2}\left(\alpha \right)+2sin\left(\alpha \right)cos\left(\alpha \right)$. Finally, we factor out $sin\left(\alpha \right)$ and multiply by $-1$:

$0=sin\left(\alpha \right)\left(2sin\left(\alpha \right)-cos\left(\alpha \right)\right)$.

$sin\left(\alpha \right)$ is only $0$ for $\alpha =0$ in $A$'s domain, which we will have to look at anyway, as it is the lower bound of the domain.

Now, $2sin\left(\alpha \right)-cos\left(\alpha \right)=0$ if and only if $2=cot\left(\alpha \right)$ if and only if $\alpha =atan\left(\frac{1}{2}\right)$.

Applying $A$ to $atan\left(\frac{1}{2}\right)$ and the lower and upper bound of the domain shows that $A$ is minimal at $atan\left(\frac{1}{2}\right)$ .

Converting back to $a$ yields ${a}_{\mathrm{min}}=2cos\left(atan\left(\frac{1}{2}\right)\right)=\frac{4}{\sqrt{5}}$, where the latter equality holds according to some arcane trigonometry.

Solution

Figure 5 shows the square's and disk's intersection with optimal $a$.

Some Remarks

Now that I know the solution to the mathematical problem, the question is: does it solve my initial problem. Let us assume we have a graphic designed to fit the shape shown in figure 5. Then it will work well in every system that uses only one shape (circle or square) as a frame for the graphic in every context. However, if a system uses graphics with circle or square shaped frames depending on context, the scaling might not fit well. However, that is a problem you have with every graphic. So, I guess it does helps with the problem.

I also looked a bit on an extension of this problem, with not only a fixed disk and variable size square, but also a variable size hexagon. That makes the problem a lot more complicated, which made me stop working on it.