[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky]
[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Find circles that are tangent to three given circles (Apollonius' Problem) in Python

[Three circles and their tangent circles.]

Given three objects such as points, lines, or circles, you can try to draw other circles that are tangent to each. The case using three circles is called Apollonius' Problem. Originally these problems were studied by Euclid (ca. 300 BC) and Apollonius of Perga (ca. 262 BC - ca. 190 BC) and they were solved geometrically with straight edge and compass.

This example solves Apollonius' Problem by finding solutions to the three following equations algebraically.

    (X - X1)2 + (Y - Y1)2 = (R ± R1)2
    (X - X2)2 + (Y - Y2)2 = (R ± R2)2
    (X - X3)2 + (Y - Y3)2 = (R ± R3)2
Unfortunately solving the equations requires dividing by some terms that may be zero and in those cases the solution doesn't work. In some cases that's because some solutions are degenerate. For example, suppose the three circles have the same radii and are all tangent to the X axis. Then two of the tangent "circles" are actually lines, one along the X axis and one parallel to the X axis on the other side of the circles.

In other cases the division by zero seems to be caused by the method for solving the equations and real solutions do exist. This is a tricky problem and I haven't yet found a solution. If you figure out how to solve these equations without these problems, please let me know.

For more discussion of the problem and how to solve the equations, see:

Meanwhile this is a kind of fun program that shows the eight tangent circles for three specific circles chosen at design time. The following code shows how the program initializes its ApolloniusApp class.

class ApolloniusApp: # Create and manage the tkinter interface. def __init__(self): # Make the main interface. ... # Create three test circles. # Each circle looks like ((cx, cy), radius). self.circles = [ ((120, 80), 30), ((250, 100), 40), ((160, 200), 60), ] # Colors to draw the circles. self.fg_colors = [ 'red', 'green', 'blue', 'orange', 'lime', 'cyan', 'pink', 'purple' ] # Find the tangent circles. self.tangent_circles = find_apollonian_circles(self.circles) # Draw the solution. self.draw() # Display the window. self.window.focus_force() self.window.mainloop()

The code starts by creating its tkinter interface. That code isn't shown. (Download the example to see it.)

It then creates data for the three pre-defined circles. It also defines colors to draw the tangent circles. There are up to eight tangent circles so the program defines eight colors.

Next, the code calls the find_apollonian_circles method to find the tangent circles. See the earlier posts for information about how that method works. The code calls the following draw method to draw the circles and their tangents and then enters the tkinter main loop.

def draw(self): self.canvas.delete(tk.ALL) # Draw the tangent circles. for i in range(len(self.tangent_circles)): circle = self.tangent_circles[i] if self.chk_vars[i].get(): fg_color = self.fg_colors[i] x = circle[0][0] y = circle[0][1] radius = circle[1] self.canvas.create_oval( x - radius, y - radius, x + radius, y + radius, outline=fg_color ) # Draw the three given circles. for circle in self.circles: x = circle[0][0] y = circle[0][1] radius = circle[1] self.canvas.create_oval( x - radius, y - radius, x + radius, y + radius, outline='black', fill='light green' )

This method removes any objects from the program's canvas widget and then loops through the tangent circles. For each circle, the code creates an oval using the next color. (It uses color 0 for circle 0, color 1 for circle 1, and so forth.)

After drawing the tangent circles, the program draws the three pre-define circles filled with light green and outlined in black.

Use the check boxes to turn different tangent circles on and off.

Download the example program to see additional details.

© 2024 - 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.