# circles12.m
(Solving the 12 circles puzzle)

Years ago I ran across this little-known but interesting geometric puzzle:
Draw 12 circles in a plane so that every circle is tangent to exactly 5 others.
This is the first figure created by circles12. I can't take credit for this solution since it was published alongside the puzzle. However, the equations for the circle centers and radii are my own, as well as all the alternative solutions discussed later.
The four radii (from smallest to largest) necessary to make this solution work are approximately:
[r1 r2 r3 r4] = [1 1.4259 4.5872 12.3914]
which can be computed from the following equations:
s = csc(π/5) r1 = 1 r2 = 1/(s-1)
r3 is the largest root of ax2 - bx + c where
c = 1+2r2 b = 2(s √ c + r2 ) a = s2 - 1
r4 = r3 (s + 1)
The circles with radii r1 thru r4 are red, green, blue, and purple respectively.
Note that there is a button on the left labeled "start rotation". When you click it, the plot begins to slowly rotate, and the button label changes to "stop rotation". Of course, clicking on the stop rotation button does exactly that and the button label reverts to "start rotation". This rotation adds no value to the solution of the geometrical puzzle but was added to demonstrate how easy it is to create moving plots and some of the programming techniques required to do that.
Actually, when you start this program normally (with no arguments) this figure will start out already rotating. If you find that distracting you can include any numeric or string argument (e.g. circles12(0)) and the figure will be created in its stopped (i.e. static) configuration.
You can control the speed of the plot's rotation with the "Rotation speed" pseudo edit object below the plot. Click on the left or right side of the speed setting number to decrease or increase this number respectively. (Higher numbers rotate faster.) For more details about how to create and use a pseudo slider, see the pseudo slider object section
The number of display updates per second is displayed underneath the plot right next to the rotation speed control. This gives you a measure of the system's plotting speed. You can use this to compare the relative performance of different computers, operating systems, and Matlab versions.
Clicking on the "Help" tag in the menu box near the lower left corner of the figure opens the document you are now reading. If you right click on this Help tag instead of left clicking, the help system will open pointing at its top level instead of at the specific topic specified (i.e. the circles12 help page). Left or right clicking on the Help tag in the other three figures described below behaves similarly.
I discovered this solution when it occurred to me that since the quadratic equation used above to solve for r3 has two roots, there must be some meaning to both of them. Indeed when I used the smaller of the two roots, the solution shown here was created (which I'm calling solution 2). This is the second figure created by the circles12 program.
There is a simple modification of both solutions 1 and 2 which create new solutions. In both figures, note that the red circle is tangent to the inside of the five green circles. In each case, you can make the red circle bigger so that it is tangent to the outside of those same five green circles. Although these are distinct solutions (which I will call solutions 1a and 2a respectively), I decided that they were not unique enough to deserve to be plotted by the program.
It is amazing that there is even one solution to this puzzle, but to my eye, this solution is both the most surprising and the most beautiful. This is the third figure created by circles12, and I refer to this as the third solution.
Looking at the tangent point at the bottom where four circles meet, the radii of the red, purple, green, and blue circles are 17/11, 2.2, 3, 4 respectively.
Looking at the tangent point at the far right or far left where four circles meet, the radii of the red, purple, green, and blue are 1, 2, 3, 4 respectively. The only requirement for that sequence is that the middle two numbers have the same sum as the end two numbers, so for example, 1,3,4,6 would work equally well although it might not be as pleasing to the eye.
For the 1,2,3,4 sequence chosen here, notice that there are only 6 different circle sizes. All 3 blue circles are the same size and all 3 green circles are also the same size. Yet the red circles come in two sizes as do the purple circles. Surprising indeed! At first glance, you might think the 3 purple circles are also the same size, but the bottom one is slightly larger than the other two (r = 2.2 vs 2.0).
As a bonus, this solution looks somewhat like a face, perhaps of an alien or a bug?
For the final figure below (showing the last two solutions) instead of the usual screen capture to create a .png file, I created an svg file (scalable vector graphics) including just the plot area. To create the svg file I followed these steps:

• | Right click on the "Data" tag in the Menu box to make the menu bar visible
• | Click on "plt" in the menu bar and select "Hide/Show cursor controls"
• | Click on "plt" in the menu bar and select "Hide/Show TraceID box"
• | Click on "File" in the menu bar and select "Save As" and then select "Scalable Vector Graphics file" in the "Save as type" popup menu at the bottom

(This is a thumbnail image. Click on it to see the full resolution .svg image)
Solution 4 (the one on the left) looks similar to solution 3 but it has 120 degree rotational symmetry, whereas solution 3 has only left/right symmetry. Also solution 3 requires 6 different circle sizes whereas solution 4 has only 4 different sizes (1, 2, 3, and 4.11684397).
Solution 5 (the one on the right) also has 120-degree symmetry, and amazingly requires only 3 different circle sizes. This gives us a way to make an already difficult problem even harder: Draw 12 circles in a plane with at most 3 different sizes so that every circle is tangent to exactly 5 others.. Of the 7 solutions presented, only this last one is a solution to the new problem. The three sizes used for this last solution are: 1, 2.30930734142, and 5. If you can derive a formula to compute the exact size of the mid-sized circle you win the mathematician of the week award! You would deserve the same award for coming up with another solution different than the seven I have presented. I certainly would want to know if you have some success with either feat.
Here a few of the programming techniques used in the creation of this program:

- The code is simplified by using complex numbers to hold the xy positions of the plotted points.
- Demonstrates using prin for creating the Trace IDs.
- Demonstrates how to make circles look true (i.e. a one-to-one aspect ratio) by using a zero in the 'Pos' argument (width or height).
- Demonstrates how to use the 'Link' parameter to link all 4 figures together so that when any of them are closed, the remaining 3 figures are also closed.
- Shows how to use the funcStart function to initiate an infinite plotting loop, so that control returns to the command window while the plotting loop continues to run.
- A multi-line uicontrol button is created by using html tags in the uicontrol string property.
- For each figure we draw 24 circles with only 12 traces (i.e. each trace renders two circles, one for solution 4 and the other for solution 5. If we just concatenated z{1} (the 12 circles for solution 4) with z{2} (the 12 circles for solution 5) then there would be an unwanted line connecting each pair of circles. Too avoid that we insert 12 NaNs (NaN+ones(1,12);) between z{1} and z{2}, one NaN for each pair of circles.
  Copyright © 2024
  Paul Mennen