
Task

Implement a solution to the Problem of Apollonius   (description on Wikipedia)   which is the problem of finding the circle that is tangent to three specified circles   (colored black in the diagram below to the right).

There is an   algebraic solution   which is pretty straightforward.



The solutions to the example in the code are shown in the diagram   (below and to the right).

The red circle is "internally tangent" to all three black circles,   and the green circle is "externally tangent" to all three black circles.




T Circle
   Float x, y, r
 
   F (x, y, r)
      .x = x
      .y = y
      .r = r
 
   F String()
      R ‘Circle(x=#., y=#., r=#.)’.format(.x, .y, .r)
 
F solveApollonius(c1, c2, c3, s1, s2, s3)
   V (x1, y1, r1) = c1
   V (x2, y2, r2) = c2
   V (x3, y3, r3) = c3
 
   V v11 = 2 * x2 - 2 * x1
   V v12 = 2 * y2 - 2 * y1
   V v13 = x1 * x1 - x2 * x2 + y1 * y1 - y2 * y2 - r1 * r1 + r2 * r2
   V v14 = 2 * s2 * r2 - 2 * s1 * r1
 
   V v21 = 2 * x3 - 2 * x2
   V v22 = 2 * y3 - 2 * y2
   V v23 = x2 * x2 - x3 * x3 + y2 * y2 - y3 * y3 - r2 * r2 + r3 * r3
   V v24 = 2 * s3 * r3 - 2 * s2 * r2
 
   V w12 = v12 / v11
   V w13 = v13 / v11
   V w14 = v14 / v11
 
   V w22 = v22 / v21 - w12
   V w23 = v23 / v21 - w13
   V w24 = v24 / v21 - w14
 
   V P = -w23 / w22
   V Q = w24 / w22
   V M = -w12 * P - w13
   V n = w14 - w12 * Q
 
   V a = n * n + Q * Q - 1
   V b = 2 * M * n - 2 * n * x1 + 2 * P * Q - 2 * Q * y1 + 2 * s1 * r1
   V c = x1 * x1 + M * M - 2 * M * x1 + P * P + y1 * y1 - 2 * P * y1 - r1 * r1
 
   V D = b * b - 4 * a * c
   V rs = (-b - sqrt(D)) / (2 * a)
 
   V xs = M + n * rs
   V ys = P + Q * rs
 
   R Circle(xs, ys, rs)
 
V (c1, c2, c3) = (Circle(0, 0, 1), Circle(4, 0, 1), Circle(2, 4, 2))
print(solveApollonius(c1, c2, c3, 1, 1, 1))
print(solveApollonius(c1, c2, c3, -1, -1, -1))