Problem of Apollonius: Difference between revisions

Content deleted Content added
m →‎{{header|MUMPS}}: Use trans tag
Chkas (talk | contribs)
 
(135 intermediate revisions by 63 users not shown)
Line 1:
{{task|arithmetic operations}}
[[File:Apollonius.png|400px|Two solutions to the problem of Apollonius|right]]
 
;Task:
Implement a solution to the Problem of Apollonius ([[wp:Problem_of_Apollonius|description on wikipedia]]) which is the problem of finding the circle that is tangent to three specified circles. There is an [[wp:Problem_of_Apollonius#Algebraic_solutions|algebraic solution]] which is pretty straightforward. The solutions to the example in the code are shown in the image (the red circle is "internally tangent" to all three black circles and the green circle is "externally tangent" to all three black circles).
Implement a solution to the Problem of Apollonius   ([[wp: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   [[wp:Problem_of_Apollonius#Algebraic_solutions|algebraic solution]]   which is pretty straightforward.
[[File:Apollonius.png|200px|Two solutions to the problem of apollonius]]
 
=={{header|C}}==
 
{{trans|Java}}
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.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">T Circle((Float x, Float y, Float 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))</syntaxhighlight>
 
{{out}}
<pre>
Circle(x=2, y=2.1, r=3.9)
Circle(x=2, y=0.833333333, r=1.166666667)
</pre>
 
=={{header|Ada}}==
apollonius.ads:
<syntaxhighlight lang="ada">package Apollonius is
type Point is record
X, Y : Long_Float := 0.0;
end record;
 
type Circle is record
Center : Point;
Radius : Long_Float := 0.0;
end record;
 
type Tangentiality is (External, Internal);
 
function Solve_CCC
(Circle_1, Circle_2, Circle_3 : Circle;
T1, T2, T3 : Tangentiality := External)
return Circle;
end Apollonius;</syntaxhighlight>
apollonius.adb:
<syntaxhighlight lang="ada">with Ada.Numerics.Generic_Elementary_Functions;
 
package body Apollonius is
package Math is new Ada.Numerics.Generic_Elementary_Functions
(Long_Float);
 
function Solve_CCC
(Circle_1, Circle_2, Circle_3 : Circle;
T1, T2, T3 : Tangentiality := External)
return Circle
is
S1 : Long_Float := 1.0;
S2 : Long_Float := 1.0;
S3 : Long_Float := 1.0;
 
X1 : Long_Float renames Circle_1.Center.X;
Y1 : Long_Float renames Circle_1.Center.Y;
R1 : Long_Float renames Circle_1.Radius;
 
X2 : Long_Float renames Circle_2.Center.X;
Y2 : Long_Float renames Circle_2.Center.Y;
R2 : Long_Float renames Circle_2.Radius;
 
X3 : Long_Float renames Circle_3.Center.X;
Y3 : Long_Float renames Circle_3.Center.Y;
R3 : Long_Float renames Circle_3.Radius;
begin
if T1 = Internal then
S1 := -S1;
end if;
if T2 = Internal then
S2 := -S2;
end if;
if T3 = Internal then
S3 := -S3;
end if;
 
declare
V11 : constant Long_Float := 2.0 * X2 - 2.0 * X1;
V12 : constant Long_Float := 2.0 * Y2 - 2.0 * Y1;
V13 : constant Long_Float :=
X1 * X1 - X2 * X2 + Y1 * Y1 - Y2 * Y2 - R1 * R1 + R2 * R2;
V14 : constant Long_Float := 2.0 * S2 * R2 - 2.0 * S1 * R1;
 
V21 : constant Long_Float := 2.0 * X3 - 2.0 * X2;
V22 : constant Long_Float := 2.0 * Y3 - 2.0 * Y2;
V23 : constant Long_Float :=
X2 * X2 - X3 * X3 + Y2 * Y2 - Y3 * Y3 - R2 * R2 + R3 * R3;
V24 : constant Long_Float := 2.0 * S3 * R3 - 2.0 * S2 * R2;
 
W12 : constant Long_Float := V12 / V11;
W13 : constant Long_Float := V13 / V11;
W14 : constant Long_Float := V14 / V11;
 
W22 : constant Long_Float := V22 / V21 - W12;
W23 : constant Long_Float := V23 / V21 - W13;
W24 : constant Long_Float := V24 / V21 - W14;
 
P : constant Long_Float := -W23 / W22;
Q : constant Long_Float := W24 / W22;
M : constant Long_Float := -W12 * P - W13;
N : constant Long_Float := W14 - W12 * Q;
 
A : constant Long_Float := N * N + Q * Q - 1.0;
B : constant Long_Float :=
2.0 * M * N -
2.0 * N * X1 +
2.0 * P * Q -
2.0 * Q * Y1 +
2.0 * S1 * R1;
C : constant Long_Float :=
X1 * X1 +
M * M -
2.0 * M * X1 +
P * P +
Y1 * Y1 -
2.0 * P * Y1 -
R1 * R1;
 
D : constant Long_Float := B * B - 4.0 * A * C;
RS : constant Long_Float := (-B - Math.Sqrt (D)) / (2.0 * A);
begin
return (Center => (X => M + N * RS, Y => P + Q * RS), Radius => RS);
end;
end Solve_CCC;
end Apollonius;</syntaxhighlight>
 
example test_apollonius.adb:
<syntaxhighlight lang="ada">with Ada.Text_IO;
with Apollonius;
 
procedure Test_Apollonius is
use Apollonius;
package Long_Float_IO is new Ada.Text_IO.Float_IO (Long_Float);
 
C1 : constant Circle := (Center => (X => 0.0, Y => 0.0), Radius => 1.0);
C2 : constant Circle := (Center => (X => 4.0, Y => 0.0), Radius => 1.0);
C3 : constant Circle := (Center => (X => 2.0, Y => 4.0), Radius => 2.0);
 
R1 : Circle := Solve_CCC (C1, C2, C3, External, External, External);
R2 : Circle := Solve_CCC (C1, C2, C3, Internal, Internal, Internal);
begin
Ada.Text_IO.Put_Line ("R1:");
Long_Float_IO.Put (R1.Center.X, Aft => 3, Exp => 0);
Long_Float_IO.Put (R1.Center.Y, Aft => 3, Exp => 0);
Long_Float_IO.Put (R1.Radius, Aft => 3, Exp => 0);
Ada.Text_IO.New_Line;
Ada.Text_IO.Put_Line ("R2:");
Long_Float_IO.Put (R2.Center.X, Aft => 3, Exp => 0);
Long_Float_IO.Put (R2.Center.Y, Aft => 3, Exp => 0);
Long_Float_IO.Put (R2.Radius, Aft => 3, Exp => 0);
Ada.Text_IO.New_Line;
end Test_Apollonius;</syntaxhighlight>
 
output:
<pre>R1:
2.000 2.100 3.900
R2:
2.000 0.833 1.167</pre>
 
=={{header|ALGOL 68}}==
{{Trans|Java|Algol 68 doesn't actually do javadoc comments but they seemed useful...}}
<syntaxhighlight lang="algol68">
BEGIN # solve the problem of Apollonius - find circles tangential to 3 others #
# translation of the Java sample #
 
OP FMT = ( REAL v )STRING:
BEGIN
STRING result := fixed( v, -12, 2 );
INT start := LWB result;
WHILE IF start >= UPB result THEN FALSE ELSE result[ start ] = " " FI DO start +:= 1 OD;
result[ start : ]
END # FMT # ;
 
MODE CIRCLE = STRUCT( REAL x, y, radius );
OP TOSTRING = ( CIRCLE c )STRING:
"Circle[x=" + FMT x OF c + ",y=" + FMT y OF c + ",r=" + FMT radius OF c + "]";
 
### Solves the Problem of Apollonius (finding a circle tangential to three other
* circles in the plane). The method uses approximately 68 heavy operations
* (multiplication, division, square-roots).
* @param c1 One of the circles in the problem
* @param c2 One of the circles in the problem
* @param c3 One of the circles in the problem
* @param s1 An indication if the solution should be externally or internally
* tangent (+1/-1) to c1
* @param s2 An indication if the solution should be externally or internally
* tangent (+1/-1) to c2
* @param s3 An indication if the solution should be externally or internally
* tangent (+1/-1) to c3
* @return The circle that is tangent to c1, c2 and c3.
#
PROC solve apollonius = ( CIRCLE c1, c2, c3, INT s1, s2, s3 )CIRCLE:
BEGIN
REAL x1 = x OF c1;
REAL y1 = y OF c1;
REAL r1 = radius OF c1;
REAL x2 = x OF c2;
REAL y2 = y OF c2;
REAL r2 = radius OF c2;
REAL x3 = x OF c3;
REAL y3 = y OF c3;
REAL r3 = radius OF c3;
REAL v11 = 2*x2 - 2*x1;
REAL v12 = 2*y2 - 2*y1;
REAL v13 = x1*x1 - x2*x2 + y1*y1 - y2*y2 - r1*r1 + r2*r2;
REAL v14 = 2*s2*r2 - 2*s1*r1;
 
REAL v21 = 2*x3 - 2*x2;
REAL v22 = 2*y3 - 2*y2;
REAL v23 = x2*x2 - x3*x3 + y2*y2 - y3*y3 - r2*r2 + r3*r3;
REAL v24 = 2*s3*r3 - 2*s2*r2;
 
REAL w12 = v12/v11;
REAL w13 = v13/v11;
REAL w14 = v14/v11;
 
REAL w22 = v22/v21-w12;
REAL w23 = v23/v21-w13;
REAL w24 = v24/v21-w14;
 
REAL p = -w23/w22;
REAL q = w24/w22;
REAL m = -w12*p-w13;
REAL n = w14 - w12*q;
 
REAL a = n*n + q*q - 1;
REAL b = 2*m*n - 2*n*x1 + 2*p*q - 2*q*y1 + 2*s1*r1;
REAL c = x1*x1 + m*m - 2*m*x1 + p*p + y1*y1 - 2*p*y1 - r1*r1;
 
# Find a root of a quadratic equation. This requires the circle centers not #
# to be e.g. colinear #
REAL d = b*b-4*a*c;
REAL rs = (-b-sqrt(d))/(2*a);
REAL xs = m + n * rs;
REAL ys = p + q * rs;
 
( xs, ys, rs )
END # solve apollonius # ;
 
CIRCLE c1 = ( 0, 0, 1 );
CIRCLE c2 = ( 4, 0, 1 );
CIRCLE c3 = ( 2, 4, 2 );
 
# should output "Circle[x=2.00,y=2.10,r=3.90]" (green circle in image) #
print( ( TOSTRING solve apollonius( c1, c2, c3, 1, 1, 1 ), newline ) );
# should output "Circle[x=2.00,y=0.83,r=1.17]" (red circle in image) #
print( ( TOSTRING solve apollonius( c1, c2, c3, -1, -1,-1 ), newline ) )
 
END
</syntaxhighlight>
{{out}}
<pre>
Circle[x=2.00,y=2.10,r=3.90]
Circle[x=2.00,y=0.83,r=1.17]
</pre>
 
=={{header|Arturo}}==
 
{{trans|Nim}}
 
<syntaxhighlight lang="rebol">define :circle [x y r][]
solveApollonius: function [c1 c2 c3 s1 s2 s3][
v11: sub 2*c2\x 2*c1\x
v12: sub 2*c2\y 2*c1\y
v13: (sub (sub c1\x*c1\x c2\x*c2\x) + (sub c1\y*c1\y c2\y*c2\y) c1\r*c1\r) + c2\r*c2\r
v14: sub 2*s2*c2\r 2*s1*c1\r
 
v21: sub 2*c3\x 2*c2\x
v22: sub 2*c3\y 2*c2\y
v23: (sub (sub c2\x*c2\x c3\x*c3\x) + (sub c2\y*c2\y c3\y*c3\y) c2\r*c2\r) + c3\r*c3\r
v24: sub 2*s3*c3\r 2*s2*c2\r
 
w12: v12/v11
w13: v13/v11
w14: v14/v11
 
w22: sub v22/v21 w12
w23: sub v23/v21 w13
w24: sub v24/v21 w14
 
p: neg w23/w22
q: w24/w22
m: sub (neg w12)*p w13
n: sub w14 w12*q
 
a: dec add n*n q*q
b: add (sub 2*m*n 2*n*c1\x) + (sub 2*p*q 2*q*c1\y) 2*s1*c1\r
c: sub sub (sub (c1\x*c1\x) + m*m 2*m*c1\x) + (p*p) + c1\y*c1\y 2*p*c1\y c1\r*c1\r
 
d: (b*b)-4*a*c
rs: ((neg b)-sqrt d )/(2*a)
 
xs: m+n*rs
ys: p+q*rs
return @[xs ys rs]
]
c1: to :circle [0.0 0.0 1.0]
c2: to :circle [4.0 0.0 1.0]
c3: to :circle [2.0 4.0 2.0]
print solveApollonius c1 c2 c3 1.0 1.0 1.0
print solveApollonius c1 c2 c3 neg 1.0 neg 1.0 neg 1.0</syntaxhighlight>
 
{{out}}
 
<pre>2.0 2.1 3.9
<lang c>#include <stdio.h>
2.0 0.8333333333333333 1.166666666666667</pre>
#include <stdlib.h>
#include <math.h>
 
=={{header|AutoHotkey}}==
typedef struct {
{{libheader|GDIP}}
struct { double x, y; } center;
<syntaxhighlight lang="autohotkey">#NoEnv
double radius;
#SingleInstance, Force
} circle;
SetBatchLines, -1
 
; Uncomment if Gdip.ahk is not in your standard library
;#Include, Gdip.ahk
 
; Start gdi+
circle *circle_new(double x, double y, double rs)
If !pToken := Gdip_Startup()
{
MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
circle *r = malloc(sizeof(circle));
ExitApp
if ( r == NULL ) return NULL;
r->center.x = x;
r->center.y = y;
r->radius = rs;
return r;
}
OnExit, Exit
 
; I've added a simple new function here, just to ensure if anyone is having any problems then to make sure they are using the correct library version
void circle_print(circle *c)
If (Gdip_LibraryVersion() < 1.30)
{
MsgBox, 48, version error!, Please download the latest version of the gdi+ library
printf("Circle[x=%.2lf,y=%.2lf,r=%.2lf]\n",
ExitApp
c->center.x, c->center.y, c->radius);
}
x1:=300,y1:=500,r1:=50,x2:=200,y2:=200,r2:=150,x3:=600,y3:=400,r3:=100,s1:=-1,s2:=-1,s3:=-1,xs:=0,ys:=0,rs:=0
, Apollonius(x1,y1,r1,x2,y2,r2,x3,y3,r3,s1,s2,s3,xs,ys,rs)
, Width:=max(x1+r1 "," x2+r2 "," x3+r3 "," xs+rs)*1.1
, Height:=max(y1+r1 "," y2+r2 "," y3+r3 "," ys+rs)*1.1
 
Gui, -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs
#define DEFXYR(N) double x##N = c##N->center.x; \
Gui, Show
double y##N = c##N->center.y; \
hwnd1 := WinExist()
double r##N = c##N->radius;
, hbm := CreateDIBSection(Width, Height)
, hdc := CreateCompatibleDC()
, obm := SelectObject(hdc, hbm)
, G := Gdip_GraphicsFromHDC(hdc)
, Gdip_SetSmoothingMode(G, 4)
, bWhite := Gdip_BrushCreateSolid(0xffffffff)
, Gdip_FillRectangle(G, bWhite, 0, 0, Width, Height)
, pRed := Gdip_CreatePen(0x88ff0000, 3)
, pGreen := Gdip_CreatePen(0x8800ff00, 3)
, pBlue := Gdip_CreatePen(0x880000ff, 3)
, pBlack := Gdip_CreatePen(0x88000000, 3)
, Gdip_DrawCircle(G, pRed, x1, y1, r1)
, Gdip_DrawCircle(G, pGreen, x2, y2, r2)
, Gdip_DrawCircle(G, pBlue, x3, y3, r3)
, Gdip_DrawCircle(G, pBlack, xs, ys, rs)
, Gdip_DeletePen(pRed)
, Gdip_DeletePen(pGreen)
, Gdip_DeletePen(pBlue)
, Gdip_DeletePen(pBlack)
, UpdateLayeredWindow(hwnd1, hdc, 0, 0, Width, Height)
Return
 
GuiEscape:
circle *solveApollonius(circle *c1, circle *c2, circle *c3,
GuiClose:
int s1, int s2, int s3)
Exit:
{
SelectObject(hdc, obm)
DEFXYR(1); DEFXYR(2); DEFXYR(3);
, DeleteObject(hbm)
, DeleteDC(hdc)
, Gdip_DeleteGraphics(G)
, Gdip_Shutdown(pToken)
ExitApp
 
Apollonius(x1=300,y1=500,r1=50,x2=200,y2=200,r2=150,x3=600,y3=400,r3=100,s1=1,s2=1,s3=1,ByRef xs=0, ByRef ys=0, ByRef rs=0) {
double v11 = 2*x2 - 2*x1;
v11 double v12 := 2*y2x2 - 2*y1;x1
v12 := 2*y2 - 2*y1
double v13 = x1*x1 - x2*x2 + y1*y1 - y2*y2 - r1*r1 + r2*r2;
v13 := x1**2 - doublex2**2 v14 =+ y1**2 - y2*s2*r22 - r1**2 + r2*s1*r1;2
v14 := 2*s2*r2 - 2*s1*r1
 
v21 := 2*x3 - 2*x2
v22 := 2*y3 - 2*y2
v23 := x2**2 - x3**2 + y2**2 - y3**2 - r2**2 + r3**2
v24 := 2*s3*r3 - 2*s2*r2
 
w12 := v12/v11
w13 := v13/v11
w14 := v14/v11
 
w22 := v22/v21 - w12
w23 := v23/v21 - w13
w24 := v24/v21 - w14
 
p := -w23/w22
q := w24/w22
m := -w12*p - w13
n := w14 - w12*q
 
a := n**2 + q**2 - 1
b := 2*m*n - 2*n*x1 + 2*p*q - 2*q*y1 + 2*s1*r1
c := x1**2 + m**2 - 2*m*x1 + p**2 + y1**2 - 2*p*y1 - r1**2
 
d := b**2 - 4*a*c
rs := (-b - d**0.5)/(2*a)
xs := m + n*rs
ys := p + q*rs
}
 
; from http://rosettacode.org/wiki/Greatest_element_of_a_list#AutoHotkey
max(list) {
Loop Parse, list, `,
x := x < A_LoopField ? A_LoopField : x
Return x
}
 
; Gdip helper function
Gdip_DrawCircle(G, pPen, x, y, r) {
Return Gdip_DrawEllipse(G, pPen, x-r, y-r, r*2, r*2)
}</syntaxhighlight>
 
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
circle1$ = " 0.000, 0.000, 1.000"
circle2$ = " 4.000, 0.000, 1.000"
circle3$ = " 2.000, 4.000, 2.000"
 
subroutine ApolloniusSolver(c1$, c2$, c3$, s1, s2, s3)
x1 = int(mid(c1$, 3, 1)): y1 = int(mid(c1$, 11, 1)): r1 = int(mid(c1$, 19, 1))
x2 = int(mid(c2$, 3, 1)): y2 = int(mid(c2$, 11, 1)): r2 = int(mid(c2$, 19, 1))
x3 = int(mid(c3$, 3, 1)): y3 = int(mid(c3$, 11, 1)): r3 = int(mid(c3$, 19, 1))
 
v11 = 2 * x2 - 2 * x1
v12 = 2 * y2 - 2* y1
v13 = x1 * x1 - x2 * x2 + y1 * y1 - y2 * y2 - r1 * r1 + r2 * r2
v14 = 2 * s2 * r2 - 2 * s1 * r1
 
v21 = 2 * x3 - 2 * x2
v22 = 2 * y3 - 2 * y2
v23 = x2 * x2 - x3 * x3 + y2 * y2 - y3 * y3 - r2 * r2 + r3 * r3
v24 = 2 * s3 * r3 - 2 * s2 * r2
 
w12 = v12 / v11
w13 = v13 / v11
w14 = v14 / v11
 
w22 = v22 / v21 - w12
w23 = v23 / v21 - w13
w24 = v24 / v21 - w14
 
P = 0 - w23 / w22
Q = w24 / w22
M = 0 - w12 * P - w13
N = w14 - w12 * Q
 
a = N * N + Q * Q - 1
b = 2 * M * N - 2 * N * x1 + 2 * P * Q - 2 * Q * y1 + 2 * s1 * r1
c = x1 * x1 + M * M -2 * M * x1 + P * P + y1 * y1 - 2 * P * y1 - r1 * r1
 
D = b * b - 4 * a * c
 
Radius = (0 - b - sqr(D)) / (2 * a)
XPos = M + N * Radius
YPos = P + Q * Radius
 
print " "; XPos; ", " ; YPos; ", " ; Radius
end subroutine
 
print " x_pos y_pos radius"
print circle1$
print circle2$
print circle3$
print
print "R1: " : call ApolloniusSolver(circle1$, circle2$, circle3$, 1, 1, 1)
print "R2: " : call ApolloniusSolver(circle1$, circle2$, circle3$, -1, -1, -1)
end
</syntaxhighlight>
 
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
Note use made of array arithmetic.
<syntaxhighlight lang="bbcbasic"> DIM Circle{x, y, r}
DIM Circles{(2)} = Circle{}
Circles{(0)}.x = 0 : Circles{(0)}.y = 0 : Circles{(0)}.r = 1
Circles{(1)}.x = 4 : Circles{(1)}.y = 0 : Circles{(1)}.r = 1
Circles{(2)}.x = 2 : Circles{(2)}.y = 4 : Circles{(2)}.r = 2
@% = &2030A
REM Solution for internal circle:
PROCapollonius(Circle{}, Circles{()}, -1, -1, -1)
PRINT "Internal: x = ";Circle.x ", y = ";Circle.y ", r = ";Circle.r
REM Solution for external circle:
PROCapollonius(Circle{}, Circles{()}, 1, 1, 1)
PRINT "External: x = ";Circle.x ", y = ";Circle.y ", r = ";Circle.r
END
DEF PROCapollonius(c{}, c{()}, s0, s1, s2)
LOCAL x0, x1, x2, y0, y1, y2, r0, r1, r2, a, b, c
LOCAL u(), v(), w() : DIM u(2), v(2), w(2)
x0 = c{(0)}.x : y0 = c{(0)}.y : r0 = c{(0)}.r
x1 = c{(1)}.x : y1 = c{(1)}.y : r1 = c{(1)}.r
x2 = c{(2)}.x : y2 = c{(2)}.y : r2 = c{(2)}.r
u() = 2*y1-2*y0, x0*x0-x1*x1+y0*y0-y1*y1-r0*r0+r1*r1, 2*s1*r1-2*s0*r0
v() = 2*y2-2*y1, x1*x1-x2*x2+y1*y1-y2*y2-r1*r1+r2*r2, 2*s2*r2-2*s1*r1
w() = u() / (2*x1 - 2*x0)
u() = v() / (2*x2 - 2*x1) - w()
u() /= u(0)
w(1) -= w(0)*u(1)
w(2) -= w(0)*u(2)
a = w(2)*w(2) + u(2)*u(2) - 1
b = -2*w(1)*w(2) - 2*w(2)*x1 - 2*u(1)*u(2) - 2*u(2)*y1 + 2*s1*r1
c = x1*x1 + w(1)*w(1) + 2*w(1)*x1 + u(1)*u(1) + y1*y1 + 2*u(1)*y1 - r1*r1
c.r = (-b - SQR(b^2 - 4*a*c)) / (2*a)
c.x = c.r * w(2) - w(1)
c.y = c.r * u(2) - u(1)
ENDPROC</syntaxhighlight>
'''Output:'''
<pre>
Internal: x = 2.000, y = 0.833, r = 1.167
External: x = 2.000, y = 2.100, r = 3.900
</pre>
 
=={{header|C}}==
C99. 2D vectors are actually complex numbers. The method here is unothordox if not insane. I can't prove that it should work, though it does seem to give correct answers for test cases I tried.
<syntaxhighlight lang="c">#include <stdio.h>
#include <tgmath.h>
 
#define VERBOSE 0
#define for3 for(int i = 0; i < 3; i++)
 
typedef complex double vec;
typedef struct { vec c; double r; } circ;
 
#define re(x) creal(x)
#define im(x) cimag(x)
#define cp(x) re(x), im(x)
#define CPLX "(%6.3f,%6.3f)"
#define CPLX3 CPLX" "CPLX" "CPLX
 
double cross(vec a, vec b) { return re(a) * im(b) - im(a) * re(b); }
double abs2(vec a) { return a * conj(a); }
int apollonius_in(circ aa[], int ss[], int flip, int divert)
double v21 = 2*x3 - 2*x2;
{
double v22 = 2*y3 - 2*y2;
vec n[3], x[3], t[3], a, b, center;
double v23 = x2*x2 - x3*x3 + y2*y2 - y3*y3 - r2*r2 + r3*r3;
int s[3], iter = 0, res = 0;
double v24 = 2*s3*r3 - 2*s2*r2;
double diff = 1, diff_old = -1, axb, d, r;
for3 {
double w12 = v12/v11;
s[i] = ss[i] ? 1 : -1;
double w13 = v13/v11;
x[i] = aa[i].c;
double w14 = v14/v11;
}
while (diff > 1e-20) {
double w22 = v22/v21-w12;
a = x[0] - doublex[2], w23b = v23/v21x[1] -w13 x[2];
diff = 0;
double w24 = v24/v21-w14;
axb = -cross(a, b);
d = sqrt(abs2(a) * abs2(b) * abs2(a - b));
 
if (VERBOSE) {
const char *z = 1 + "-0+";
printf("%c%c%c|%c%c|",
z[s[0]], z[s[1]], z[s[2]], z[flip], z[divert]);
printf(CPLX3, cp(x[0]), cp(x[1]), cp(x[2]));
}
 
/* r and center represent an arc through points x[i]. Each step,
we'll deform this arc by pushing or pulling some point on it
towards the edge of each given circle. */
r = fabs(d / (2 * axb));
center = (abs2(a)*b - abs2(b)*a) / (2 * axb) * I + x[2];
/* maybe the "arc" is actually straight line; then we have two
double P = -w23/w22;
choices in defining "push" and "pull", so try both */
double Q = w24/w22;
if (!axb && flip != -1 && !divert) {
double M = -w12*P-w13;
if (!d) { /* generally means circle centers overlap */
double N = w14 - w12*Q;
printf("Given conditions confused me.\n");
return 0;
}
 
if (VERBOSE) puts("\n[divert]");
divert = 1;
res = apollonius_in(aa, ss, -1, 1);
}
 
/* if straight line, push dir is its norm; else it's away from center */
for3 n[i] = axb ? aa[i].c - center : a * I * flip;
for3 t[i] = aa[i].c + n[i] / cabs(n[i]) * aa[i].r * s[i];
 
/* diff: how much tangent points have moved since last iteration */
for3 diff += abs2(t[i] - x[i]), x[i] = t[i];
 
if (VERBOSE) printf(" %g\n", diff);
/* keep an eye on the total diff: failing to converge means no solution */
double a = N*N + Q*Q - 1;
if (diff >= diff_old && diff_old >= 0)
double b = 2*M*N - 2*N*x1 + 2*P*Q - 2*Q*y1 + 2*s1*r1;
if (iter++ > 20) return res;
double c = x1*x1 + M*M - 2*M*x1 + P*P + y1*y1 - 2*P*y1 - r1*r1;
 
diff_old = diff;
double D = b*b-4*a*c;
}
double rs = (-b-sqrt(D))/(2*a);
double xs = M+N*rs;
double ys = P+Q*rs;
 
printf("found: ");
circle *r = malloc(sizeof(circle));
if (axb) printf("circle "CPLX", r = %f\n", cp(center), r);
r->center.x = xs;
else printf("line "CPLX3"\n", cp(x[0]), cp(x[1]), cp(x[2]));
r->center.y = ys;
r->radius = rs;
 
return res + 1;
return r;
}
int apollonius(circ aa[])
{
int s[3], i, sum = 0;
for (i = 0; i < 8; i++) {
s[0] = i & 1, s[1] = i & 2, s[2] = i & 4;
 
/* internal or external results of a zero-radius circle are the same */
if (s[0] && !aa[0].r) continue;
if (s[1] && !aa[1].r) continue;
if (s[2] && !aa[2].r) continue;
sum += apollonius_in(aa, s, 1, 0);
}
return sum;
}
int main()
{
circ a[3] = {{0, 1}, {4, 1}, {2 + 4 * I, 1}};
circle *c1 = circle_new(0.0, 0.0, 1.0);
circ b[3] = {{-3, 2}, {0, 1}, {3, 2}};
circle *c2 = circle_new(4.0, 0.0, 1.0);
circ c[3] = {{-2, 1}, {0, 1}, {2 * I, 1}};
circle *c3 = circle_new(2.0, 4.0, 2.0);
//circ c[3] = {{0, 1}, {0, 2}, {0, 3}}; <-- a fun one
puts("set 1"); apollonius(a);
puts("set 2"); apollonius(b);
puts("set 3"); apollonius(c);
}</syntaxhighlight>
 
=={{header|C sharp|C#}}==
This code finds all 8 possible circles touching the three given circles.
<syntaxhighlight lang="csharp">
using System;
 
namespace ApolloniusProblemCalc
{
class Program
{
static float rs = 0;
static float xs = 0;
static float ys = 0;
 
public static void Main(string[] args)
{
float gx1;
float gy1;
float gr1;
float gx2;
float gy2;
float gr2;
float gx3;
float gy3;
float gr3;
 
//----------Enter values for the given circles here----------
gx1 = 0;
gy1 = 0;
gr1 = 1;
gx2 = 4;
gy2 = 0;
gr2 = 1;
gx3 = 2;
gy3 = 4;
gr3 = 2;
//-----------------------------------------------------------
 
for (int i = 1; i <= 8; i++)
{
SolveTheApollonius(i, gx1, gy1, gr1, gx2, gy2, gr2, gx3, gy3, gr3);
 
 
if (i == 1)
{
Console.WriteLine("X of point of the " + i + "st solution: " + xs.ToString());
Console.WriteLine("Y of point of the " + i + "st solution: " + ys.ToString());
Console.WriteLine(i + "st Solution circle's radius: " + rs.ToString());
}
else if (i == 2)
{
Console.WriteLine("X of point of the " + i + "ed solution: " + xs.ToString());
Console.WriteLine("Y of point of the " + i + "ed solution: " + ys.ToString());
Console.WriteLine(i + "ed Solution circle's radius: " + rs.ToString());
}
else if(i == 3)
{
Console.WriteLine("X of point of the " + i + "rd solution: " + xs.ToString());
Console.WriteLine("Y of point of the " + i + "rd solution: " + ys.ToString());
Console.WriteLine(i + "rd Solution circle's radius: " + rs.ToString());
}
else
{
Console.WriteLine("X of point of the " + i + "th solution: " + xs.ToString());
Console.WriteLine("Y of point of the " + i + "th solution: " + ys.ToString());
Console.WriteLine(i + "th Solution circle's radius: " + rs.ToString());
}
 
Console.WriteLine();
}
 
 
Console.ReadKey(true);
}
 
private static void SolveTheApollonius(int calcCounter, float x1, float y1, float r1, float x2, float y2, float r2, float x3, float y3, float r3)
{
float s1 = 1;
float s2 = 1;
float s3 = 1;
 
if (calcCounter == 2)
{
s1 = -1;
s2 = -1;
s3 = -1;
}
else if (calcCounter == 3)
{
s1 = 1;
s2 = -1;
s3 = -1;
}
else if (calcCounter == 4)
{
s1 = -1;
s2 = 1;
s3 = -1;
}
else if (calcCounter == 5)
{
s1 = -1;
s2 = -1;
s3 = 1;
}
else if (calcCounter == 6)
{
s1 = 1;
s2 = 1;
s3 = -1;
}
else if (calcCounter == 7)
{
s1 = -1;
s2 = 1;
s3 = 1;
}
else if (calcCounter == 8)
{
s1 = 1;
s2 = -1;
s3 = 1;
}
 
//This calculation to solve for the solution circles is cited from the Java version
float v11 = 2 * x2 - 2 * x1;
float v12 = 2 * y2 - 2 * y1;
float v13 = x1 * x1 - x2 * x2 + y1 * y1 - y2 * y2 - r1 * r1 + r2 * r2;
float v14 = 2 * s2 * r2 - 2 * s1 * r1;
 
float v21 = 2 * x3 - 2 * x2;
float v22 = 2 * y3 - 2 * y2;
float v23 = x2 * x2 - x3 * x3 + y2 * y2 - y3 * y3 - r2 * r2 + r3 * r3;
float v24 = 2 * s3 * r3 - 2 * s2 * r2;
 
float w12 = v12 / v11;
float w13 = v13 / v11;
float w14 = v14 / v11;
 
float w22 = v22 / v21 - w12;
float w23 = v23 / v21 - w13;
float w24 = v24 / v21 - w14;
 
float P = -w23 / w22;
float Q = w24 / w22;
float M = -w12 * P - w13;
float N = w14 - w12 * Q;
 
float a = N * N + Q * Q - 1;
float b = 2 * M * N - 2 * N * x1 + 2 * P * Q - 2 * Q * y1 + 2 * s1 * r1;
float c = x1 * x1 + M * M - 2 * M * x1 + P * P + y1 * y1 - 2 * P * y1 - r1 * r1;
 
float D = b * b - 4 * a * c;
 
rs = (-b - float.Parse(Math.Sqrt(D).ToString())) / (2 * float.Parse(a.ToString()));
xs = M + N * rs;
ys = P + Q * rs;
}
}
}
</syntaxhighlight>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <cmath>
#include <iostream>
#include <stdexcept>
 
struct Circle {
double x;
double y;
double radius;
 
void display() {
std::cout << "centre(" << x << ", " << y << ")" << ", radius = " << radius << std::endl;
}
};
 
double square(const double& value) {
return value * value;
}
 
/**
* The parameters s1, s2, s3 indicate whether the result circle is
* internally tangent to the corresponding argument circle (-1), or
* externally tangent to the corresponding argument circle (+1).
*/
Circle solve_apollonius(const Circle& circle1, const Circle& circle2, const Circle& circle3,
const double& s1, const double& s2, const double& s3) {
if ( abs(s1) != 1 || abs(s2) != 1 || abs(s3) != 1 ) {
throw std::invalid_argument("Parameters s1, s2, s3 must be +1 or -1");
}
 
const double v11 = 2 * circle2.x - 2 * circle1.x;
const double v12 = 2 * circle2.y - 2 * circle1.y;
const double v13 = square(circle1.x) - square(circle2.x) + square(circle1.y) - square(circle2.y)
- square(circle1.radius) + square(circle2.radius);
const double v14 = 2 * s2 * circle2.radius - 2 * s1 * circle1.radius;
 
const double v21 = 2 * circle3.x - 2 * circle2.x;
const double v22 = 2 * circle3.y - 2 * circle2.y;
const double v23 = square(circle2.x) - square(circle3.x) + square(circle2.y) - square(circle3.y)
- square(circle2.radius) + square(circle3.radius);
const double v24 = 2 * s3 * circle3.radius - 2 * s2 * circle2.radius;
 
const double w12 = v12 / v11;
const double w13 = v13 / v11;
const double w14 = v14 / v11;
 
const double w22 = v22 / v21 - w12;
const double w23 = v23 / v21 - w13;
const double w24 = v24 / v21 - w14;
 
const double p = -w23 / w22;
const double q = w24 / w22;
const double m = -w12 * p - w13;
const double n = w14 - w12 * q;
 
const double a = square(n) + square(q) - 1;
const double b = 2 * m * n - 2 * n * circle1.x + 2 * p * q - 2 * q * circle1.y + 2 * s1 * circle1.radius;
const double c = square(circle1.x) + square(m) - 2 * m * circle1.x + square(p)
+ square(circle1.y) - 2 * p * circle1.y - square(circle1.radius);
 
const double discriminant = b * b - 4 * a * c;
const double root = ( -b - sqrt(discriminant) ) / ( 2 * a );
 
const double centre_x = m + n * root;
const double centre_y = p + q * root;
 
return Circle(centre_x, centre_y, root);
}
 
int main() {
Circle circle1(0.0, 0.0, 1.0);
Circle circle2(4.0, 0.0, 1.0);
Circle circle3(2.0, 4.0, 2.0);
 
std::cout << "The three initial circles are:" << std::endl;
std::cout << " Circle 1: "; circle1.display();
std::cout << " Circle 2: "; circle2.display();
std::cout << " Circle 3: "; circle3.display();
std::cout << std::endl;
std::cout << "The internal circle is: "; solve_apollonius(circle1, circle2, circle3, -1.0, -1.0, -1.0).display();
std::cout << "The external circle is: "; solve_apollonius(circle1, circle2, circle3, 1.0, 1.0, 1.0).display();
}
</syntaxhighlight>
{{ out }}
<pre>
The three initial circles are:
Circle 1: centre(0, 0), radius = 1
Circle 2: centre(4, 0), radius = 1
Circle 3: centre(2, 4), radius = 2
 
The internal circle is: centre(2, 0.833333), radius = 1.16667
The external circle is: centre(2, 2.1), radius = 3.9
</pre>
 
=={{header|CoffeeScript}}==
{{trans|Java}}
<syntaxhighlight lang="coffeescript">
class Circle
constructor: (@x, @y, @r) ->
apollonius = (c1, c2, c3, s1=1, s2=1, s3=1) ->
[x1, y1, r1] = [c1.x, c1.y, c1.r]
[x2, y2, r2] = [c2.x, c2.y, c2.r]
[x3, y3, r3] = [c3.x, c3.y, c3.r]
sq = (n) -> n*n
circle *r1 = solveApollonius(c1, c2, c3, 1, 1, 1);
 
circle_print(r1);
v11 = 2*x2 - 2*x1
circle *r2 = solveApollonius(c1, c2, c3, -1, -1, -1);
v12 = 2*y2 - 2*y1
circle_print(r2);
v13 = sq(x1) - sq(x2) + sq(y1) - sq(y2) - sq(r1) + sq(r2)
v14 = 2*s2*r2 - 2*s1*r1
 
v21 = 2*x3 - 2*x2
v22 = 2*y3 - 2*y2
v23 = sq(x2) - sq(x3) + sq(y2) - sq(y3) - sq(r2) + sq(r3)
v24 = 2*s3*r3 - 2*s2*r2
 
w12 = v12/v11
w13 = v13/v11
w14 = v14/v11
 
w22 = v22/v21 - w12
w23 = v23/v21 - w13
w24 = v24/v21 - w14
 
p = -w23/w22
q = w24/w22
m = -w12*p - w13
n = w14 - w12*q
 
a = sq(n) + sq(q) - 1
b = 2*m*n - 2*n*x1 + 2*p*q - 2*q*y1 + 2*s1*r1
c = sq(x1) + sq(m) - 2*m*x1 + sq(p) + sq(y1) - 2*p*y1 - sq(r1)
 
d = sq(b) - 4*a*c
rs = (-b - Math.sqrt(d)) / (2*a)
xs = m + n*rs
ys = p + q*rs
new Circle(xs, ys, rs)
free(c1); free(c2); free(c3);
free(r1); free(r2);
}</lang>
console.log c1 = new Circle(0, 0, 1)
console.log c2 = new Circle(2, 4, 2)
console.log c3 = new Circle(4, 0, 1)
console.log apollonius(c1, c2, c3)
console.log apollonius(c1, c2, c3, -1, -1, -1)
 
</syntaxhighlight>
=={{header|Java}}==
output
{{lines too long}}
<syntaxhighlight lang="text">
<lang Java>public class Circle{
> coffee foo.coffee
public double[] center;
{ x: 0, y: 0, r: 1 }
public double radius;
{ x: 2, y: 4, r: 2 }
public Circle(double[] center, double radius){
{ x: 4, y: 0, r: 1 }
this.center = center;
{ x: 2, y: this2.radius1, =r: radius;3.9 }
{ x: 2, y: 0.8333333333333333, r: 1.1666666666666667 }
}
</syntaxhighlight>
public String toString(){ return String.format("Circle[x=%.2f,y=%.2f,r=%.2f]",center[0],center[1],radius); }
 
=={{header|D}}==
{{trans|Java}}
<syntaxhighlight lang="d">import std.stdio, std.math;
 
immutable struct Circle { double x, y, r; }
enum Tangent { externally, internally }
 
/**
Solves the Problem of Apollonius (finding a circle tangent to three
other circles in the plane).
 
Params:
c1 = First circle of the problem.
c2 = Second circle of the problem.
c3 = Third circle of the problem.
t1 = How is the solution tangent (externally or internally) to c1.
t2 = How is the solution tangent (externally or internally) to c2.
t3 = How is the solution tangent (externally or internally) to c3.
 
Returns: The Circle that is tangent to c1, c2 and c3.
*/
Circle solveApollonius(in Circle c1, in Circle c2, in Circle c3,
in Tangent t1, in Tangent t2, in Tangent t3)
pure nothrow @safe @nogc {
alias Imd = immutable(double);
Imd s1 = (t1 == Tangent.externally) ? 1.0 : -1.0;
Imd s2 = (t2 == Tangent.externally) ? 1.0 : -1.0;
Imd s3 = (t3 == Tangent.externally) ? 1.0 : -1.0;
 
Imd v11 = 2 * c2.x - 2 * c1.x;
Imd v12 = 2 * c2.y - 2 * c1.y;
Imd v13 = c1.x ^^ 2 - c2.x ^^ 2 +
c1.y ^^ 2 - c2.y ^^ 2 -
c1.r ^^ 2 + c2.r ^^ 2;
Imd v14 = 2 * s2 * c2.r - 2 * s1 * c1.r;
 
Imd v21 = 2 * c3.x - 2 * c2.x;
Imd v22 = 2 * c3.y - 2 * c2.y;
Imd v23 = c2.x ^^ 2 - c3.x ^^ 2 +
c2.y ^^ 2 - c3.y ^^ 2 -
c2.r ^^ 2 + c3.r ^^ 2;
Imd v24 = 2 * s3 * c3.r - 2 * s2 * c2.r;
 
Imd w12 = v12 / v11;
Imd w13 = v13 / v11;
Imd w14 = v14 / v11;
 
Imd w22 = v22 / v21 - w12;
Imd w23 = v23 / v21 - w13;
Imd w24 = v24 / v21 - w14;
 
Imd P = -w23 / w22;
Imd Q = w24 / w22;
Imd M = -w12 * P - w13;
Imd N = w14 - w12 * Q;
 
Imd a = N * N + Q ^^ 2 - 1;
Imd b = 2 * M * N - 2 * N * c1.x +
2 * P * Q - 2 * Q * c1.y +
2 * s1 * c1.r;
Imd c = c1.x ^^ 2 + M ^^ 2 - 2 * M * c1.x +
P ^^ 2 + c1.y ^^ 2 - 2 * P * c1.y - c1.r ^^ 2;
 
// find a root of a quadratic equation.
// This requires the circle centers not to be e.g. colinear
Imd D = b ^^ 2 - 4 * a * c;
Imd rs = (-b - D.sqrt) / (2 * a);
 
return Circle(M + N * rs, P + Q * rs, rs);
}
 
void main() {
public class ApolloniusSolver {
immutable c1 = Circle(0.0, 0.0, 1.0);
/** Solves the Problem of Apollonius (finding a circle tangent to three other circles in the plane).
immutable c2 = Circle(4.0, 0.0, 1.0);
* The method uses approximately 68 heavy operations (multiplication, division, square-roots).
immutable c3 = Circle(2.0, 4.0, 2.0);
* @param c1 One of the circles in the problem
 
* @param c2 One of the circles in the problem
alias Te = Tangent.externally;
* @param c3 One of the circles in the problem
solveApollonius(c1, c2, c3, Te, Te, Te).writeln;
* @param s1 An indication if the solution should be externally or internally tangent (+1/-1) to c1
 
* @param s2 An indication if the solution should be externally or internally tangent (+1/-1) to c2
alias Ti = Tangent.internally;
* @param s3 An indication if the solution should be externally or internally tangent (+1/-1) to c3
solveApollonius(c1, c2, c3, Ti, Ti, Ti).writeln;
* @return The circle that is tangent to c1, c2 and c3.
}</syntaxhighlight>
*/
{{out}}
public static Circle solveApollonius(Circle c1, Circle c2, Circle c3, int s1, int s2, int s3){
<pre>immutable(Circle)(2, 2.1, 3.9)
float x1 = c1.center[0];
immutable(Circle)(2, 0.833333, 1.16667)
float y1 = c1.center[1];
</pre>
float r1 = c1.radius;
 
float x2 = c2.center[0];
=={{header|Dyalect}}==
float y2 = c2.center[1];
{{trans|Swift}}
float r2 = c2.radius;
<syntaxhighlight lang="dyalect">type Circle(Array center, Float radius) with Lookup
float x3 = c3.center[0];
func Circle.ToString() =>
float y3 = c3.center[1];
"Circle[x=\(this.center[0]),y=\(this.center[1]),r=\(this.radius)]"
float r3 = c3.radius;
func solveApollonius(Circle c1, Circle c2, Circle c3, Float s1, Float s2, Float s3) {
let x1 = c1.center[0]
let y1 = c1.center[1]
let r1 = c1.radius
let x2 = c2.center[0]
let y2 = c2.center[1]
let r2 = c2.radius
let x3 = c3.center[0]
let y3 = c3.center[1]
let r3 = c3.radius
let v11 = 2.0 * x2 - 2.0 * x1
let v12 = 2.0 * y2 - 2.0 *y1
let v13 = x1 * x1 - x2 * x2 + y1 * y1 - y2 * y2 - r1 * r1 + r2 * r2
let v14 = 2.0 * s2 * r2 - 2.0 * s1 * r1
let v21 = 2.0 * x3 - 2.0 * x2
let v22 = 2.0 * y3 - 2.0 * y2
let v23 = x2 * x2 - x3 * x3 + y2 * y2 - y3 * y3 - r2 * r2 + r3 * r3
let v24 = 2.0 * s3 * r3 - 2 * s2 * r2
let w12 = v12 / v11
let w13 = v13 / v11
let w14 = v14 / v11
let w22 = v22 / v21-w12
let w23 = v23 / v21-w13
let w24 = v24 / v21-w14
let p = -w23 / w22
let q = w24 / w22
let m = -w12 * p - w13
let n = w14 - w12 * q
let a = n * n + q * q - 1.0
let b = 2.0 * m * n - 2.0 * n * x1 + 2 * p * q - 2.0 * q * y1 + 2.0 * s1 * r1
let c = x1 * x1 + m * m - 2.0 * m * x1 + p * p + y1 * y1 - 2.0 * p * y1 - r1 * r1
let d = b * b - 4.0 * a * c
let rs = (-b - sqrt(d)) / (2.0 * a)
let xs = m + n * rs
let ys = p + q * rs
Circle(center: [xs,ys], radius: rs)
}
let c1 = Circle(center: [0.0, 0.0], radius: 1.0)
let c2 = Circle(center: [4.0, 0.0], radius: 1.0)
let c3 = Circle(center: [2.0, 4.0], radius: 2.0)
print(solveApollonius(c1, c2, c3, 1.0, 1.0, 1.0))
print(solveApollonius(c1, c2, c3, -1.0, -1.0, -1.0))</syntaxhighlight>
 
{{out}}
 
<pre>Circle[x=2,y=2.1,r=3.9]
Circle[x=2,y=0.8333333333333333,r=1.1666666666666667]</pre>
 
=={{header|EasyLang}}==
[https://easylang.online/show/#cod=bVRNb5tAFLzvrxgllwQE3g/aWKq45WrLOVsc8JqmVl3jLtSGf1+99xabRJEttDNv3uzwgD2H1qNrj5cG3mwreEsXt63QGXQWnUOOsK2QKwDH5sTAERgMSmoyFX5gjMASCBG4inWWkI26CEQXQdQ5Qi7qIhBdBKJ7BF0vhjaxSMg/k4WRgo2FcSqMsUA2g2ElMurj5pTCkwgZtcS+QFwwSCllgmDFoojenZBxg07ELLG3XG7KJb32lmsqjLHAuW63MjhpTmdpRjf1xTSUi7jgxOKWS8gp1z36lcdCw1nQ7IRyTLk5VTBVzCjOTekXfHMZWUmB262bFZwU2MQWs0JBhQ1KZFduuFr2eEPJ8huxYomh3Ju745pkppC9keCNyBol1kiwRoo3IpGBE+/iLFZclEms5amnEGdRs5U8+vTzc/SzdyVlq1XsWE3khq3mr89kziDczeSVfUWJHRLskKFAghoJPBVChxJPGfHd39Dj9RkLPJFZ/cx1+iZKrJDyfYROSEvkJt79RDoiQ6dyxd9ziS00NAwqxd82EcVEuEhYFLCo1JcHgeFfMNtKncPh1MvyS2lm4j/Yu5qWj1BnOmn8IXgMGBGQy5HSN0OPhwda/mwDP1SNvoX7romjQ+dwajAgRXc48cwCRqTwbSeIZLnKZYN9qK/w7RGezizZwrfHNtCVASXwNNAERiOF0/A0yjl0AlWudrX//R7af6c9lsul8semDooSXQ/7/hd0/k2JvdZa0ebv4bBXf9pLQ06apbQyWgur4SJrNK85sdaaZzlD9gNyNPJj05x5S+FfdHwUH/kXrWXo6j8= Run it]
{{trans|Java}}
<syntaxhighlight lang=text>
proc solve c1[] c2[] c3[] s1 s2 s3 . r[] .
len r[] 3
x1 = c1[1] ; y1 = c1[2] ; r1 = c1[3]
x2 = c2[1] ; y2 = c2[2] ; r2 = c2[3]
x3 = c3[1] ; y3 = c3[2] ; r3 = c3[3]
#
v11 = 2 * x2 - 2 * x1
v12 = 2 * y2 - 2 * y1
v13 = x1 * x1 - x2 * x2 + y1 * y1 - y2 * y2 - r1 * r1 + r2 * r2
v14 = 2 * s2 * r2 - 2 * s1 * r1
v21 = 2 * x3 - 2 * x2
v22 = 2 * y3 - 2 * y2
v23 = x2 * x2 - x3 * x3 + y2 * y2 - y3 * y3 - r2 * r2 + r3 * r3
v24 = 2 * s3 * r3 - 2 * s2 * r2
w12 = v12 / v11
w13 = v13 / v11
w14 = v14 / v11
w22 = v22 / v21 - w12
w23 = v23 / v21 - w13
w24 = v24 / v21 - w14
P = -w23 / w22
Q = w24 / w22
M = -w12 * P - w13
N = w14 - w12 * Q
a = N * N + Q * Q - 1
b = 2 * M * N - 2 * N * x1 + 2 * P * Q - 2 * Q * y1 + 2 * s1 * r1
c = x1 * x1 + M * M - 2 * M * x1 + P * P + y1 * y1 - 2 * P * y1 - r1 * r1
#
D = b * b - 4 * a * c
rs = (-b - sqrt D) / (2 * a)
r[1] = M + N * rs
r[2] = P + Q * rs
r[3] = rs
.
c1[] = [ 0 0 1 ]
c2[] = [ 4 0 1 ]
c3[] = [ 2 4 2 ]
solve c1[] c2[] c3[] 1 1 1 r1[]
print r1[]
solve c1[] c2[] c3[] -1 -1 -1 r2[]
print r2[]
#
proc circ x y r . .
text ""
for a = 0 to 360
line x + sin a * r y + cos a * r
.
.
proc draw col c[] . .
color col
circ c[1] * 10 + 30 c[2] * 10 + 30 c[3] * 10
.
background 888
clear
linewidth 0.5
color 000
drawgrid
move 30 0
line 30 100
move 0 30
line 100 30
draw 000 c1[]
draw 000 c2[]
draw 000 c3[]
sleep 0.5
draw 070 r1[]
sleep 0.5
draw 700 r2[]
</syntaxhighlight>
 
{{out}}
<pre>
[ 2 2.10 3.90 ]
[ 2 0.83 1.17 ]
</pre>
 
=={{header|Elixir}}==
{{trans|Ruby}}
<syntaxhighlight lang="elixir">defmodule Circle do
def apollonius(c1, c2, c3, s1, s2, s3) do
{x1, y1, r1} = c1
{w12, w13, w14} = calc(c1, c2, s1, s2)
{u22, u23, u24} = calc(c2, c3, s2, s3)
{w22, w23, w24} = {u22 - w12, u23 - w13, u24 - w14}
p = -w23 / w22
//Currently optimized for fewest multiplications. Should be optimized for readability
float v11q = 2*x2w24 -/ 2*x1;w22
float v12m = 2-w12 *y2 p - 2*y1;w13
float v13n = x1*x1w14 - x2*x2w12 + y1*y1 - y2*y2 - r1*r1 + r2*r2;q
float v14 = 2*s2*r2 - 2*s1*r1;
float v21a = 2n*x3n -+ 2q*x2;q - 1
float v22b = 2*y3m*n - 2*y2;n*x1 + 2*p*q - 2*q*y1 + 2*s1*r1
float v23c = x2x1*x2x1 + m*m - x32*x3m*x1 + y2p*y2p -+ y3y1*y3y1 - r22*r2p*y1 +- r3r1*r3;r1
float v24 = 2*s3*r3 - 2*s2*r2;
float w12d = v12/v11;b*b - 4*a*c
rs = (-b - :math.sqrt(d)) / (2*a)
float w13 = v13/v11;
float{m w14+ =n*rs, v14/v11;p + q*rs, rs}
end
defp calc({x1, y1, r1}, {x2, y2, r2}, s1, s2) do
v1 = x2 - x1
{(y2 - y1) / v1, (x1*x1 - x2*x2 + y1*y1 - y2*y2 - r1*r1 + r2*r2) / (2*v1), (s2*r2 - s1*r1) / v1}
end
end
 
c1 = {0, 0, 1}
c2 = {2, 4, 2}
c3 = {4, 0, 1}
IO.inspect Circle.apollonius(c1, c2, c3, 1, 1, 1)
IO.inspect Circle.apollonius(c1, c2, c3, -1, -1, -1)</syntaxhighlight>
 
{{out}}
<pre>
{2.0, 2.1, 3.9}
{2.0, 0.8333333333333333, 1.1666666666666667}
</pre>
 
=={{header|F_Sharp|F#}}==
{{trans|OCaml}}
<syntaxhighlight lang="fsharp">type point = { x:float; y:float }
type circle = { center: point; radius: float; }
 
let new_circle x y r =
{ center = { x=x; y=y }; radius = r }
let print_circle c =
printfn "Circle(x=%.2f, y=%.2f, r=%.2f)"
c.center.x c.center.y c.radius
let xyr c = c.center.x, c.center.y, c.radius
let solve_apollonius c1 c2 c3
s1 s2 s3 =
 
let x1, y1, r1 = xyr c1
let x2, y2, r2 = xyr c2
let x3, y3, r3 = xyr c3
let v11 = 2. * x2 - 2. * x1
let v12 = 2. * y2 - 2. * y1
let v13 = x1*x1 - x2*x2 + y1*y1 - y2*y2 - r1*r1 + r2*r2
let v14 = (2. * s2 * r2) - (2. * s1 * r1)
let v21 = 2. * x3 - 2. * x2
let v22 = 2. * y3 - 2. * y2
let v23 = x2*x2 - x3*x3 + y2*y2 - y3*y3 - r2*r2 + r3*r3
let v24 = (2. * s3 * r3) - (2. * s2 * r2)
let w12 = v12 / v11
let w13 = v13 / v11
let w14 = v14 / v11
let w22 = v22 / v21 - w12
let w23 = v23 / v21 - w13
let w24 = v24 / v21 - w14
let p = - w23 / w22
let q = w24 / w22
let m = - w12 * p - w13
let n = w14 - w12 * q
let a = n*n + q*q - 1.
let b = 2.*m*n - 2.*n*x1 + 2.*p*q - 2.*q*y1 + 2.*s1*r1
let c = x1*x1 + m*m - 2.*m*x1 + p*p + y1*y1 - 2.*p*y1 - r1*r1
let d = b * b - 4. * a * c
let rs = (- b - (sqrt d)) / (2. * a)
let xs = m + n * rs
let ys = p + q * rs
new_circle xs ys rs
 
[<EntryPoint>]
let main argv =
let c1 = new_circle 0. 0. 1.
let c2 = new_circle 4. 0. 1.
let c3 = new_circle 2. 4. 2.
let r1 = solve_apollonius c1 c2 c3 1. 1. 1.
print_circle r1
let r2 = solve_apollonius c1 c2 c3 (-1.) (-1.) (-1.)
print_circle r2
0</syntaxhighlight>
{{out}}
<pre>Circle(x=2.00, y=2.10, r=3.90)
Circle(x=2.00, y=0.83, r=1.17)</pre>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<syntaxhighlight lang="fortran">program Apollonius
implicit none
 
integer, parameter :: dp = selected_real_kind(15)
 
type circle
real(dp) :: x
real(dp) :: y
real(dp) :: radius
end type
type(circle) :: c1 , c2, c3, r
 
c1 = circle(0.0, 0.0, 1.0)
c2 = circle(4.0, 0.0, 1.0)
c3 = circle(2.0, 4.0, 2.0)
 
write(*, "(a,3f12.8))") "External tangent:", SolveApollonius(c1, c2, c3, 1, 1, 1)
write(*, "(a,3f12.8))") "Internal tangent:", SolveApollonius(c1, c2, c3, -1, -1, -1)
contains
 
function SolveApollonius(c1, c2, c3, s1, s2, s3) result(res)
type(circle) :: res
type(circle), intent(in) :: c1, c2, c3
integer, intent(in) :: s1, s2, s3
real(dp) :: x1, x2, x3, y1, y2, y3, r1, r2, r3
real(dp) :: v11, v12, v13, v14
real(dp) :: v21, v22, v23, v24
real(dp) :: w12, w13, w14
real(dp) :: w22, w23, w24
real(dp) :: p, q, m, n, a, b, c, det
x1 = c1%x; x2 = c2%x; x3 = c3%x
y1 = c1%y; y2 = c2%y; y3 = c3%y
r1 = c1%radius; r2 = c2%radius; r3 = c3%radius
 
v11 = 2*x2 - 2*x1
v12 = 2*y2 - 2*y1
v13 = x1*x1 - x2*x2 + y1*y1 - y2*y2 - r1*r1 + r2*r2
v14 = 2*s2*r2 - 2*s1*r1
v21 = 2*x3 - 2*x2
v22 = 2*y3 - 2*y2
v23 = x2*x2 - x3*x3 + y2*y2 - y3*y3 - r2*r2 + r3*r3
v24 = 2*s3*r3 - 2*s2*r2
w12 = v12/v11
w13 = v13/v11
w14 = v14/v11
w22 = v22/v21-w12
w23 = v23/v21-w13
w24 = v24/v21-w14
p = -w23/w22
q = w24/w22
m = -w12*P - w13
n = w14 - w12*q
a = n*n + q*q - 1
b = 2*m*n - 2*n*x1 + 2*p*q - 2*q*y1 + 2*s1*r1
c = x1*x1 + m*m - 2*m*x1 + p*p + y1*y1 - 2*p*y1 - r1*r1
det = b*b - 4*a*c
res%radius = (-b-sqrt(det)) / (2*a)
res%x = m + n*res%radius
res%y = p + q*res%radius
 
end function
end program</syntaxhighlight>
Output
<pre>External tangent: 2.00000000 2.10000000 3.90000000
Internal tangent: 2.00000000 0.83333333 1.16666667</pre>
 
 
=={{header|FreeBASIC}}==
{{trans|Liberty BASIC}}
<syntaxhighlight lang="freebasic">
Dim As String circle1 = " 0.000, 0.000, 1.000"
Dim As String circle2 = " 4.000, 0.000, 1.000"
Dim As String circle3 = " 2.000, 4.000, 2.000"
 
Sub ApolloniusSolver(c1 As String, c2 As String, c3 As String, s1 As Single, s2 As Single, s3 As Single)
Dim As Single x1, x2, x3, y1, y2, y3, r1, r2, r3
Dim As Single v11, v12, v13, v14, v21, v22, v23, v24, w12, w13, w14
Dim As Single w22, w23, w24,P, Q, M, N, a, b, c, D
Dim As Single Radius, XPos, YPos
x1 = Val(Mid(c1, 3, 1)): y1 = Val(Mid(c1, 11, 1)): r1 = Val(Mid(c1, 19, 1))
float w22 = v22/v21-w12;
x2 = Val(Mid(c2, 3, 1)): y2 = Val(Mid(c2, 11, 1)): r2 = Val(Mid(c2, 19, 1))
float w23 = v23/v21-w13;
x3 = Val(Mid(c3, 3, 1)): y3 = Val(Mid(c3, 11, 1)): r3 = Val(Mid(c3, 19, 1))
float w24 = v24/v21-w14;
float Pv11 = 2 * x2 -w23/w22; 2 * x1
float Qv12 = w24/w22;2 * y2 - 2* y1
v13 = x1 * x1 - x2 * x2 + y1 * y1 - y2 * y2 - r1 * r1 + r2 * r2
float M = -w12*P-w13;
float Nv14 = w142 * s2 * r2 - w122 *Q; s1 * r1
float av21 = N*N2 + Q*Q x3 - 1;2 * x2
float bv22 = 2*M*N - 2*N*x1 + 2*P*Qy3 - 2 *Q*y1 + 2*s1*r1;y2
float cv23 = x1*x1x2 + M*M x2 - 2x3 *M*x1 x3 + Py2 *P +y2 - y3 y1*y1 y3 - 2*Pr2 *y1 -r2 + r3 r1*r1; r3
v24 = 2 * s3 * r3 - 2 * s2 * r2
w12 = v12 / v11
// Find a root of a quadratic equation. This requires the circle centers not to be e.g. colinear
float Dw13 = b*b-4*a*c;v13 / v11
float rsw14 = (-b-Math.sqrt(D))v14 /(2*a); v11
float xsw22 = M+N*rs;v22 / v21 - w12
float ysw23 = P+Q*rs;v23 / v21 - w13
w24 = v24 / v21 - w14
P = 0 - w23 / w22
return new Circle(new double[]{xs,ys}, rs);
Q = w24 / w22
}
M = 0 - w12 * P - w13
N = w14 - w12 * Q
a = N * N + Q * Q - 1
b = 2 * M * N - 2 * N * x1 + 2 * P * Q - 2 * Q * y1 + 2 * s1 * r1
c = x1 * x1 + M * M -2 * M * x1 + P * P + y1 * y1 - 2 * P * y1 - r1 * r1
D = b * b - 4 * a * c
Radius = (0 - b - Sqr(D)) / (2 * a)
XPos = M + N * Radius
YPos = P + Q * Radius
Print Using " ##.###, ##.###, ##.###"; XPos; YPos; Radius
End Sub
 
Print " x_pos y_pos radius"
Print circle1
Print circle2
Print circle3
Print
Print "R1: " : ApolloniusSolver(circle1, circle2, circle3, 1, 1, 1)
Print "R2: " : ApolloniusSolver(circle1, circle2, circle3, -1, -1, -1)
Sleep
</syntaxhighlight>
{{out}}
<pre>
x_pos y_pos radius
0.000, 0.000, 1.000
4.000, 0.000, 1.000
2.000, 4.000, 2.000
 
R1:
2.000, 2.100, 3.900
R2:
2.000, 0.833, 1.167
</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">Problem of Apollonius
 
include "NSLog.incl"
 
begin record Circle
CGPoint center
double radius
CFStringRef locator
end record
 
local fn CircleToString( c as Circle ) as CFStringRef
end fn = fn StringWithFormat( @"%@ Circle( x = %0.3f, y = %0.3f, radius = %0.3f )", c.locator, c.center.x, c.center.y, c.radius )
 
local fn SolveApollonius( c1 as Circle, c2 as Circle, c3 as Circle, s1 as Double, s2 as Double, s3 as Double ) as Circle
'~'1
Circle result
 
double x1 = c1.center.x
double y1 = c1.center.y
double r1 = c1.radius
 
double x2 = c2.center.x
double y2 = c2.center.y
double r2 = c2.radius
 
double x3 = c3.center.x
double y3 = c3.center.y
double r3 = c3.radius
 
double v11 = 2*x2 - 2*x1
double v12 = 2*y2 - 2*y1
double v13 = x1*x1 - x2*x2 + y1*y1 - y2*y2 - r1*r1 + r2*r2
double v14 = 2*s2*r2 - 2*s1*r1
 
double v21 = 2*x3 - 2*x2
double v22 = 2*y3 - 2*y2
double v23 = x2*x2 - x3*x3 + y2*y2 - y3*y3 - r2*r2 + r3*r3
double v24 = 2*s3*r3 - 2*s2*r2
 
double w12 = v12/v11
double w13 = v13/v11
double w14 = v14/v11
 
double w22 = v22/v21-w12
double w23 = v23/v21-w13
double w24 = v24/v21-w14
 
double P = -w23/w22
double Q = w24/w22
double M = -w12*P-w13
double N = w14 - w12*Q
 
double a = N*N + Q*Q - 1
double b = 2*M*N - 2*N*x1 + 2*P*Q - 2*Q*y1 + 2*s1*r1
double c = x1*x1 + M*M - 2*M*x1 + P*P + y1*y1 - 2*P*y1 - r1*r1
 
double D = b*b-4*a*c
 
double rs = (-b - sqr(D)) / (2*a)
double xs = M + N * rs
double ys = P + Q * rs
 
result.center.x = xs
result.center.y = ys
result.radius = rs
 
if ( s1 < 1 )
result.locator = @"Internal Tangent:"
else
result.locator = @"External Tangent:"
end if
end fn = result
 
Circle c1, c2, c3, c
 
c1.center.x = 0.0 : c1.center.y = 0.0 : c1.radius = 1.0
c2.center.x = 4.0 : c2.center.y = 0.0 : c2.radius = 1.0
c3.center.x = 2.0 : c3.center.y = 4.0 : c3.radius = 2.0
 
// External tangent
c = fn SolveApollonius( c1, c2, c3, 1, 1, 1 )
NSLog( @"%@", fn CircleToString( c ) )
 
// Internal tangent
c = fn SolveApollonius( c1, c2, c3, -1, -1, -1 )
NSLog( @"%@", fn CircleToString( c ) )
 
HandleEvents
</syntaxhighlight>
Output
<pre>External Tangent: Circle( x = 2.000, y = 2.100, radius = 3.900 )
Internal Tangent: Circle( x = 2.000, y = 0.833, radius = 1.167 )</pre>
 
=={{header|Go}}==
{{trans|Java}}
Simplified to produce only the fully interior and fully exterior solutions.
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"math"
)
 
type circle struct {
x, y, r float64
}
 
func main() {
c1 := circle{0, 0, 1}
c2 := circle{4, 0, 1}
c3 := circle{2, 4, 2}
fmt.Println(ap(c1, c2, c3, true))
fmt.Println(ap(c1, c2, c3, false))
}
 
func ap(c1, c2, c3 circle, s bool) circle {
x1sq := c1.x * c1.x
y1sq := c1.y * c1.y
r1sq := c1.r * c1.r
x2sq := c2.x * c2.x
y2sq := c2.y * c2.y
r2sq := c2.r * c2.r
x3sq := c3.x * c3.x
y3sq := c3.y * c3.y
r3sq := c3.r * c3.r
v11 := 2 * (c2.x - c1.x)
v12 := 2 * (c2.y - c1.y)
v13 := x1sq - x2sq + y1sq - y2sq - r1sq + r2sq
v14 := 2 * (c2.r - c1.r)
v21 := 2 * (c3.x - c2.x)
v22 := 2 * (c3.y - c2.y)
v23 := x2sq - x3sq + y2sq - y3sq - r2sq + r3sq
v24 := 2 * (c3.r - c2.r)
if s {
v14 = -v14
v24 = -v24
}
w12 := v12 / v11
w13 := v13 / v11
w14 := v14 / v11
w22 := v22/v21 - w12
w23 := v23/v21 - w13
w24 := v24/v21 - w14
p := -w23 / w22
q := w24 / w22
m := -w12*p - w13
n := w14 - w12*q
a := n*n + q*q - 1
b := m*n - n*c1.x + p*q - q*c1.y
if s {
b -= c1.r
} else {
b += c1.r
}
b *= 2
c := x1sq + m*m - 2*m*c1.x + p*p + y1sq - 2*p*c1.y - r1sq
d := b*b - 4*a*c
rs := (-b - math.Sqrt(d)) / (2 * a)
return circle{m + n*rs, p + q*rs, rs}
}</syntaxhighlight>
Output:
<pre>
{2 0.8333333333333333 1.1666666666666667}
{2 2.1 3.9}
</pre>
 
=={{header|Haskell}}==
{{trans|D}}
<syntaxhighlight lang="haskell">data Circle = Circle { x, y, r :: Double } deriving (Show, Eq)
data Tangent = Externally | Internally deriving Eq
 
{--
Solves the Problem of Apollonius (finding a circle tangent to three
other circles in the plane).
 
Params:
c1 = First circle of the problem.
c2 = Second circle of the problem.
c3 = Third circle of the problem.
t1 = How is the solution tangent (externally or internally) to c1.
t2 = How is the solution tangent (externally or internally) to c2.
t3 = How is the solution tangent (externally or internally) to c3.
 
Returns: The Circle that is tangent to c1, c2 and c3.
--}
solveApollonius :: Circle -> Circle -> Circle ->
Tangent -> Tangent -> Tangent ->
Circle
solveApollonius c1 c2 c3 t1 t2 t3 =
Circle (m + n * rs) (p + q * rs) rs
where
s1 = if t1 == Externally then 1.0 else -1.0
s2 = if t2 == Externally then 1.0 else -1.0
s3 = if t3 == Externally then 1.0 else -1.0
 
v11 = 2 * x c2 - 2 * x c1
v12 = 2 * y c2 - 2 * y c1
v13 = x c1 ^ 2 - x c2 ^ 2 +
y c1 ^ 2 - y c2 ^ 2 -
r c1 ^ 2 + r c2 ^ 2
v14 = 2 * s2 * r c2 - 2 * s1 * r c1
 
v21 = 2 * x c3 - 2 * x c2
v22 = 2 * y c3 - 2 * y c2
v23 = x c2 ^ 2 - x c3 ^ 2 +
y c2 ^ 2 - y c3 ^ 2 -
r c2 ^ 2 + r c3 ^ 2;
v24 = 2 * s3 * r c3 - 2 * s2 * r c2
 
w12 = v12 / v11
w13 = v13 / v11
w14 = v14 / v11
 
w22 = v22 / v21 - w12
w23 = v23 / v21 - w13
w24 = v24 / v21 - w14
 
p = -w23 / w22
q = w24 / w22
m = -w12 * p - w13
n = w14 - w12 * q
 
a = n * n + q ^ 2 - 1
b = 2 * m * n - 2 * n * x c1 +
2 * p * q - 2 * q * y c1 +
2 * s1 * r c1
c = x c1 ^ 2 + m ^ 2 - 2 * m * x c1 +
p ^ 2 + y c1 ^ 2 - 2 * p * y c1 - r c1 ^ 2
 
-- Find a root of a quadratic equation.
-- This requires the circle centers not to be e.g. colinear.
d = b ^ 2 - 4 * a * c
rs = (-b - sqrt d) / (2 * a)
 
main = do
let c1 = Circle 0.0 0.0 1.0
let c2 = Circle 4.0 0.0 1.0
let c3 = Circle 2.0 4.0 2.0
let te = Externally
print $ solveApollonius c1 c2 c3 te te te
 
let ti = Internally
print $ solveApollonius c1 c2 c3 ti ti ti</syntaxhighlight>
{{out}}
<pre>Circle {x = 2.0, y = 2.1, r = 3.9}
Circle {x = 2.0, y = 0.8333333333333333, r = 1.1666666666666667}</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
This is a translation of the Java version. [[File:Apollonius-unicon.png|thumb|Solution for Apollonius]]
 
<syntaxhighlight lang="icon">link graphics
 
record circle(x,y,r)
global scale,xoffset,yoffset,yadjust
 
procedure main()
 
WOpen("size=400,400") | stop("Unable to open Window")
scale := 28
xoffset := WAttrib("width") / 2
yoffset := ( yadjust := WAttrib("height")) / 2
 
 
WC(c1 := circle(0,0,1),"black")
WC(c2 := circle(4,0,1),"black")
WC(c3 := circle(2,4,2),"black")
WC(c4 := Apollonius(c1,c2,c3,1,1,1),"green") #/ Expects "Circle[x=2.00,y=2.10,r=3.90]" (green circle in image)
WC(c5 := Apollonius(c1,c2,c3,-1,-1,-1),"red") #/ Expects "Circle[x=2.00,y=0.83,r=1.17]" (red circle in image)
 
 
WAttrib("fg=blue")
DrawLine( 0*scale+xoffset, yadjust-(-1*scale+yoffset), 0*scale+xoffset, yadjust-(4*scale+yoffset) )
DrawLine( -1*scale+xoffset, yadjust-(0*scale+yoffset), 4*scale+xoffset, yadjust-(0*scale+yoffset) )
WDone()
end
 
procedure WC(c,fg) # write and plot circle
WAttrib("fg="||fg)
DrawCircle(c.x*scale+xoffset, yadjust-(c.y*scale+yoffset), c.r*scale)
return write("Circle(x,y,r) := (",c.x,", ",c.y,", ",c.r,")")
end
 
procedure Apollonius(c1,c2,c3,s1,s2,s3) # solve Apollonius
 
v11 := 2.*(c2.x - c1.x)
v12 := 2.*(c2.y - c1.y)
v13 := c1.x^2 - c2.x^2 + c1.y^2 - c2.y^2 - c1.r^2 + c2.r^2
v14 := 2.*(s2*c2.r - s1*c1.r)
v21 := 2.*(c3.x - c2.x)
v22 := 2.*(c3.y - c2.y)
v23 := c2.x^2 - c3.x^2 + c2.y^2 - c3.y^2 - c2.r^2 + c3.r^2
v24 := 2.*(s3*c3.r - s2*c2.r)
w12 := v12/v11
w13 := v13/v11
w14 := v14/v11
w22 := v22/v21-w12
w23 := v23/v21-w13
w24 := v24/v21-w14
P := -w23/w22
Q := w24/w22
M := -w12*P-w13
N := w14 - w12*Q
a := N*N + Q*Q - 1
b := 2*M*N - 2*N*c1.x + 2*P*Q - 2*Q*c1.y + 2*s1*c1.r
c := c1.x*c1.x + M*M - 2*M*c1.x + P*P + c1.y*c1.y - 2*P*c1.y - c1.r*c1.r
#// Find a root of a quadratic equation. This requires the circle centers not to be e.g. colinear
D := b*b-4*a*c
rs := (-b-sqrt(D))/(2*a)
xs := M + N * rs
ys := P + Q * rs
return circle(xs,ys,rs)
end</syntaxhighlight>
 
Output:<pre>Circle(x,y,r) := (0, 0, 1)
Circle(x,y,r) := (4, 0, 1)
Circle(x,y,r) := (2, 4, 2)
Circle(x,y,r) := (2.0, 2.1, 3.9)
Circle(x,y,r) := (2.0, 0.8333333333333333, 1.166666666666667)</pre>
 
=={{header|J}}==
'''Solution'''
<syntaxhighlight lang="j">require 'math/misc/amoeba'
 
NB.*apollonius v solves Apollonius problems
NB. y is Cx0 Cy0 R0, Cx1 Cy1 R1,: Cx2 Cy2 R2
NB. x are radius scale factors to control which circles are included
NB. in the common tangent circle. 1 to surround, _1 to exclude.
NB. returns Cxs Cys Rs
apollonius =: verb define"1 _
1 apollonius y
:
centers=. 2{."1 y
radii=. x * {:"1 y
goal=. 1e_20 NB. goal simplex volume
dist=. radii + [: +/"1&.:*: centers -"1 ] NB. distances to tangents
'soln err'=. ([: +/@:*:@, -/~@dist) f. amoeba goal centers
if. err > 10 * goal do. '' return. end. NB. no solution found
avg=. +/ % #
(, avg@dist) soln
)</syntaxhighlight>
 
'''Usage'''
<syntaxhighlight lang="j"> ]rctst=: 0 0 1,4 0 1,:2 4 2 NB. Task circles
0 0 1
4 0 1
2 4 2
(_1 _1 _1 ,: 1 1 1) apollonius rctst NB. internally & externally tangent solutions
2 0.83333333 1.1666667
2 2.1 3.9
</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">public class Circle
{
public double[] center;
public double radius;
public Circle(double[] center, double radius)
{
this.center = center;
this.radius = radius;
}
public String toString()
{
return String.format("Circle[x=%.2f,y=%.2f,r=%.2f]",center[0],center[1],
radius);
}
}
 
public class ApolloniusSolver
{
/** Solves the Problem of Apollonius (finding a circle tangent to three other
* circles in the plane). The method uses approximately 68 heavy operations
* (multiplication, division, square-roots).
* @param c1 One of the circles in the problem
* @param c2 One of the circles in the problem
* @param c3 One of the circles in the problem
* @param s1 An indication if the solution should be externally or internally
* tangent (+1/-1) to c1
* @param s2 An indication if the solution should be externally or internally
* tangent (+1/-1) to c2
* @param s3 An indication if the solution should be externally or internally
* tangent (+1/-1) to c3
* @return The circle that is tangent to c1, c2 and c3.
*/
public static Circle solveApollonius(Circle c1, Circle c2, Circle c3, int s1,
int s2, int s3)
{
float x1 = c1.center[0];
float y1 = c1.center[1];
float r1 = c1.radius;
float x2 = c2.center[0];
float y2 = c2.center[1];
float r2 = c2.radius;
float x3 = c3.center[0];
float y3 = c3.center[1];
float r3 = c3.radius;
 
//Currently optimized for fewest multiplications. Should be optimized for
//readability
float v11 = 2*x2 - 2*x1;
float v12 = 2*y2 - 2*y1;
float v13 = x1*x1 - x2*x2 + y1*y1 - y2*y2 - r1*r1 + r2*r2;
float v14 = 2*s2*r2 - 2*s1*r1;
 
float v21 = 2*x3 - 2*x2;
float v22 = 2*y3 - 2*y2;
float v23 = x2*x2 - x3*x3 + y2*y2 - y3*y3 - r2*r2 + r3*r3;
float v24 = 2*s3*r3 - 2*s2*r2;
 
float w12 = v12/v11;
float w13 = v13/v11;
float w14 = v14/v11;
 
float w22 = v22/v21-w12;
float w23 = v23/v21-w13;
float w24 = v24/v21-w14;
 
float P = -w23/w22;
float Q = w24/w22;
float M = -w12*P-w13;
float N = w14 - w12*Q;
 
float a = N*N + Q*Q - 1;
float b = 2*M*N - 2*N*x1 + 2*P*Q - 2*Q*y1 + 2*s1*r1;
float c = x1*x1 + M*M - 2*M*x1 + P*P + y1*y1 - 2*P*y1 - r1*r1;
 
// Find a root of a quadratic equation. This requires the circle centers not
// to be e.g. colinear
float D = b*b-4*a*c;
float rs = (-b-Math.sqrt(D))/(2*a);
float xs = M + N * rs;
float ys = P + Q * rs;
return new Circle(new double[]{xs,ys}, rs);
}
public static void main(final String[] args)
{
Circle c1 = new Circle(new double[]{0,0}, 1);
Circle c2 = new Circle(new double[]{4,0}, 1);
Circle c3 = new Circle(new double[]{2,4}, 2);
// Expects "Circle[x=2.00,y=2.10,r=3.90]" (green circle in image)
System.out.println(solveApollonius(c1,c2,c3,1,1,1));
// Expects "Circle[x=2.00,y=0.83,r=1.17]" (red circle in image)
System.out.println(solveApollonius(c1,c2,c3,-1,-1,-1));
}
}</syntaxhighlight>
 
=={{header|jq}}==
{{trans|Go}}
{{works with|jq|1.4}}
<syntaxhighlight lang="jq">def circle:
{"x": .[0], "y": .[1], "r": .[2]};
 
# Find the interior or exterior Apollonius circle of three circles:
# ap(circle, circle, circle, boolean)
# Specify s as true for interior; false for exterior
def ap(c1; c2; c3; s):
def sign: if s then -. else . end;
(c1.x * c1.x) as $x1sq
| (c1.y * c1.y) as $y1sq
| (c1.r * c1.r) as $r1sq
| (c2.x * c2.x) as $x2sq
| (c2.y * c2.y) as $y2sq
| (c2.r * c2.r) as $r2sq
| (c3.x * c3.x) as $x3sq
| (c3.y * c3.y) as $y3sq
| (c3.r * c3.r) as $r3sq
 
| (2 * (c2.x - c1.x)) as $v11
| (2 * (c2.y - c1.y)) as $v12
| ($x1sq - $x2sq + $y1sq - $y2sq - $r1sq + $r2sq) as $v13
| (2 * (c2.r - c1.r) | sign) as $v14
| (2 * (c3.x - c2.x)) as $v21
| (2 * (c3.y - c2.y)) as $v22
 
| ($x2sq - $x3sq + $y2sq - $y3sq - $r2sq + $r3sq) as $v23
| ( 2 * c3.r - c2.r | sign) as $v24
| ($v12 / $v11) as $w12
| ($v13 / $v11) as $w13
| ($v14 / $v11) as $w14
 
| (($v22 / $v21) - $w12) as $w22
| (($v23 / $v21) - $w13) as $w23
| (($v24 / $v21) - $w14) as $w24
 
| (-$w23 / $w22) as $p
| ( $w24 / $w22) as $q
| ((-$w12*$p) - $w13) as $m
| ( $w14 - ($w12*$q)) as $n
 
| ( $n*$n + $q*$q - 1 ) as $a
| (2 * (($m*$n - $n*c1.x + $p*$q - $q*c1.y) + (c1.r|sign))) as $b
| ($x1sq + $m*$m - 2*$m*c1.x + $p*$p + $y1sq - 2*$p*c1.y - $r1sq) as $c
 
| ( $b*$b - 4*$a*$c ) as $d # discriminant
| (( -$b - (($d|sqrt))) / (2 * $a)) as $rs # root
 
| [$m + ($n*$rs), $p + ($q*$rs), $rs]
| circle
;</syntaxhighlight>
'''The task''':
<syntaxhighlight lang="jq">def task:
([0, 0, 1] | circle) as $c1
| ([4, 0, 1] | circle) as $c2
| ([2, 4, 2] | circle) as $c3
| ( ap($c1; $c2; $c3; true), # interior
ap($c1; $c2; $c3; false) ) # exterior
;</syntaxhighlight>
{{Out}}
<syntaxhighlight lang="sh">$ jq -n -c -f apollonius.jq
{"x":2,"y":0.8333333333333333,"r":1.1666666666666667}
{"x":2,"y":2.1,"r":3.9}</syntaxhighlight>
 
=={{header|Julia}}==
This solution follows the algebraic solution from [http://mathworld.wolfram.com/ApolloniusProblem.html Weisstein, Eric W. "Apollonius' Problem." From MathWorld--A Wolfram Web Resource]. The [https://github.com/Keno/Polynomials.jl Polynomials] package is used to solve the quadratic equation for the radius (equation 1 in the reference) rather than hard coding it.
 
The <code>enc</code> array passed to the <code>apollonius</code> function, specifies which of the three defining circles are to be enclosed in the solution. For this task only the "internal" (<code>enc=[]</code>) and "external" (<code>enc=[1:3]</code>) are called for.
 
'''Module''':
<syntaxhighlight lang="julia">
 
module ApolloniusProblems
 
using Polynomials, LinearAlgebra, Printf
export Circle
 
struct Point{T<:Real}
x::T
y::T
end
 
xcoord(p::Point) = p.x
ycoord(p::Point) = p.y
 
struct Circle{T<:Real}
c::Point{T}
r::T
end
Circle(x::T, y::T, r::T) where T<:Real = Circle(Point(x, y), r)
 
radius(c::Circle) = c.r
center(c::Circle) = c.c
xcenter(c::Circle) = xcoord(center(c))
ycenter(c::Circle) = ycoord(center(c))
 
Base.show(io::IO, c::Circle) =
@printf(io, "centered at (%0.4f, %0.4f) with radius %0.4f",
xcenter(c), ycenter(c), radius(c))
 
function solve(ap::Vector{Circle{T}}, enc=()) where T<:Real
length(ap) == 3 || error("This Apollonius problem needs 3 circles.")
x = @. xcenter(ap)
y = @. ycenter(ap)
r = map(u -> ifelse(u ∈ enc, -1, 1), 1:3) .* radius.(ap)
@views begin
a = 2x[1] .- 2x[2:3]
b = 2y[1] .- 2y[2:3]
c = 2r[1] .- 2r[2:3]
d = (x[1] ^ 2 + y[1] ^ 2 - r[1] ^ 2) .- (x[2:3] .^ 2 .+ y[2:3] .^ 2 .- r[2:3] .^ 2)
end
u = Polynomial([-det([b d]), det([b c])] ./ det([a b]))
v = Polynomial([det([a d]), -det([a c])] ./ det([a b]))
w = Polynomial([r[1], 1.0]) ^ 2
s = (u - x[1]) ^ 2 + (v - y[1]) ^ 2 - w
r = filter(x -> iszero(imag(x)) && x > zero(x), roots(s))
length(r) < 2 || error("The solution is not unique.")
length(r) == 1 || error("There is no solution.")
r = r[1]
return Circle(evalpoly(r, u), evalpoly(r, v), r)
end
 
end # module ApolloniusProblem</syntaxhighlight>
 
'''Main''':
<syntaxhighlight lang="julia">include("module.jl")
using .ApolloniusProblems
 
let test = [Circle(0.0, 0.0, 1.0), Circle(4.0, 0.0, 1.0), Circle(2.0, 4.0, 2.0)]
println("The defining circles are: \n - ", join(test, "\n - "))
println("The internal circle is:\n\t", ApolloniusProblems.solve(test))
println("The external circle is:\n\t", ApolloniusProblems.solve(test, 1:3))
end</syntaxhighlight>
 
{{out}}
<pre>The defining circles are:
- centered at (0.0000, 0.0000) with radius 1.0000
- centered at (4.0000, 0.0000) with radius 1.0000
- centered at (2.0000, 4.0000) with radius 2.0000
The internal circle is:
centered at (2.0000, 0.8333) with radius 1.1667
The external circle is:
centered at (2.0000, 2.1000) with radius 3.9000</pre>
 
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="scala">// version 1.1.3
 
data class Circle(val x: Double, val y: Double, val r: Double)
 
val Double.sq get() = this * this
 
fun solveApollonius(c1: Circle, c2: Circle, c3: Circle,
s1: Int, s2: Int, s3: Int): Circle {
val (x1, y1, r1) = c1
val (x2, y2, r2) = c2
val (x3, y3, r3) = c3
 
val v11 = 2 * x2 - 2 * x1
val v12 = 2 * y2 - 2 * y1
val v13 = x1.sq - x2.sq + y1.sq - y2.sq - r1.sq + r2.sq
val v14 = 2 * s2 * r2 - 2 * s1 * r1
val v21 = 2 * x3 - 2 * x2
val v22 = 2 * y3 - 2 * y2
val v23 = x2.sq - x3.sq + y2.sq - y3.sq - r2.sq + r3.sq
val v24 = 2 * s3 * r3 - 2 * s2 * r2
val w12 = v12 / v11
val w13 = v13 / v11
val w14 = v14 / v11
val w22 = v22 / v21 - w12
val w23 = v23 / v21 - w13
val w24 = v24 / v21 - w14
val p = -w23 / w22
val q = w24 / w22
val m = -w12 * p - w13
val n = w14 - w12 * q
val a = n.sq + q.sq - 1
val b = 2 * m * n - 2 * n * x1 + 2 * p * q - 2 * q * y1 + 2 * s1 * r1
val c = x1.sq + m.sq - 2 * m * x1 + p.sq + y1.sq - 2 * p * y1 - r1.sq
val d = b.sq - 4 * a * c
val rs = (-b - Math.sqrt(d)) / (2 * a)
val xs = m + n * rs
val ys = p + q * rs
return Circle(xs, ys, rs)
}
 
fun main(args: Array<String>) {
val c1 = Circle(0.0, 0.0, 1.0)
val c2 = Circle(4.0, 0.0, 1.0)
val c3 = Circle(2.0, 4.0, 2.0)
println(solveApollonius(c1, c2, c3, 1, 1, 1))
println(solveApollonius(c1, c2, c3,-1,-1,-1))
}</syntaxhighlight>
 
{{out}}
<pre>
Circle(x=2.0, y=2.1, r=3.9)
Circle(x=2.0, y=0.8333333333333333, r=1.1666666666666667)
</pre>
 
=={{header|Lasso}}==
{{trans|Java}}
<syntaxhighlight lang="lasso">define solveApollonius(c1, c2, c3, s1, s2, s3) => {
local(
x1 = decimal(#c1->get(1)),
y1 = decimal(#c1->get(2)),
r1 = decimal(#c1->get(3))
)
local(
x2 = decimal(#c2->get(1)),
y2 = decimal(#c2->get(2)),
r2 = decimal(#c2->get(3))
)
local(
x3 = decimal(#c3->get(1)),
y3 = decimal(#c3->get(2)),
r3 = decimal(#c3->get(3))
)
local(
v11 = 2*#x2 - 2*#x1,
v12 = 2*#y2 - 2*#y1,
v13 = #x1*#x1 - #x2*#x2 + #y1*#y1 - #y2*#y2 - #r1*#r1 + #r2*#r2,
v14 = 2*#s2*#r2 - 2*#s1*#r1,
v21 = 2*#x3 - 2*#x2,
v22 = 2*#y3 - 2*#y2,
v23 = #x2*#x2 - #x3*#x3 + #y2*#y2 - #y3*#y3 - #r2*#r2 + #r3*#r3,
v24 = 2*#s3*#r3 - 2*#s2*#r2,
w12 = #v12/#v11,
w13 = #v13/#v11,
w14 = #v14/#v11,
w22 = #v22/#v21-#w12,
w23 = #v23/#v21-#w13,
w24 = #v24/#v21-#w14,
P = -#w23/#w22,
Q = #w24/#w22,
M = -#w12*#P-#w13,
N = #w14 - #w12*#Q,
a = #N*#N + #Q*#Q - 1,
b = 2*#M*#N - 2*#N*#x1 + 2*#P*#Q - 2*#Q*#y1 + 2*#s1*#r1,
c = #x1*#x1 + #M*#M - 2*#M*#x1 + #P*#P + #y1*#y1 - 2*#P*#y1 - #r1*#r1
 
)
// Find a root of a quadratic equation. This requires the circle centers not to be e.g. colinear
local(
D = #b*#b-4*#a*#c,
rs = (-#b - #D->sqrt)/(2*#a),
xs = #M+#N*#rs,
ys = #P+#Q*#rs
)
return (:#xs, #ys, #rs)
}
// Tests:
solveApollonius((:0, 0, 1), (:4, 0, 1), (:2, 4, 2), 1,1,1)
solveApollonius((:0, 0, 1), (:4, 0, 1), (:2, 4, 2), -1,-1,-1)
</syntaxhighlight>
{{out}}
<pre>staticarray(2.000000, 2.100000, 3.900000)
staticarray(2.000000, 0.833333, 1.166667)</pre>
 
=={{header|Liberty BASIC}}==
Uses the string Circle$ to hold "xPos, yPos, radius" as csv data. A GUI representation is very easily added.
<syntaxhighlight lang="lb">
circle1$ =" 0.000, 0.000, 1.000"
circle2$ =" 4.000, 0.000, 1.000"
circle3$ =" 2.000, 4.000, 2.000"
 
print " x_pos y_pos radius"
print circle1$
print circle2$
print circle3$
print
print ApolloniusSolver$( circle1$, circle2$, circle3$, 1, 1, 1)
print ApolloniusSolver$( circle1$, circle2$, circle3$, -1, -1, -1)
 
end
 
function ApolloniusSolver$( c1$, c2$, c3$, s1, s2, s3)
x1 =val( word$( c1$, 1, ",")): y1 =val( word$( c1$, 2, ",")): r1 =val( word$( c1$, 3, ","))
x2 =val( word$( c2$, 1, ",")): y2 =val( word$( c2$, 2, ",")): r2 =val( word$( c2$, 3, ","))
x3 =val( word$( c3$, 1, ",")): y3 =val( word$( c3$, 2, ",")): r3 =val( word$( c3$, 3, ","))
 
v11 = 2 *x2 -2 *x1
v12 = 2 *y2 -2*y1
v13 = x1 *x1 - x2 *x2 + y1 *y1 - y2 *y2 -r1 *r1 +r2 *r2
v14 = 2 *s2 *r2 -2 *s1 *r1
 
v21 = 2 *x3 -2 *x2
v22 = 2 *y3 -2*y2
v23 = x2 *x2 -x3 *x3 + y2 *y2 -y3 *y3 -r2 *r2 +r3 *r3
v24 = 2 *s3 *r3 - 2 *s2 *r2
 
w12 = v12 /v11
w13 = v13 /v11
w14 = v14 /v11
 
w22 = v22 /v21 -w12
w23 = v23 /v21 -w13
w24 = v24 /v21 -w14
 
P = 0 -w23 /w22
Q = w24 /w22
M = 0 -w12 *P -w13
N = w14 -w12 *Q
 
a = N *N + Q *Q -1
b = 2 *M *N -2 *N *x1 + 2 *P *Q -2 *Q *y1 +2 *s1 *r1
c = x1 *x1 +M *M -2 *M *x1 +P *P +y1 *y1 -2 *P *y1 -r1 *r1
 
D = b *b -4 *a *c
 
Radius =( 0 -b -Sqr( D)) /( 2 *a)
XPos =M +N *Radius
YPos =P +Q *Radius
 
ApolloniusSolver$ =using( "###.###", XPos) +"," +using( "###.###", YPos) +using( "###.###", Radius)
end function
</syntaxhighlight>
x_pos y_pos radius
0.000, 0.000, 1.000
4.000, 0.000, 1.000
2.000, 4.000, 2.000
 
2.000, 2.100, 3.900
2.000, 0.833, 1.167
 
=={{header|Lua}}==
 
<syntaxhighlight lang="lua">
function solveApollonius(x1, y1, r1, x2, y2, r2, x3, y3, r3, s1, s2, s3)
local v11 = 2*x2 - 2*x1
local v12 = 2*y2 - 2*y1
local v13 = x1*x1 - x2*x2 + y1*y1 - y2*y2 - r1*r1 + r2*r2
local v14 = 2*s2*r2 - 2*s1*r1
 
local v21 = 2*x3 - 2*x2
local v22 = 2*y3 - 2*y2
local v23 = x2*x2 - x3*x3 + y2*y2 - y3*y3 - r2*r2 + r3*r3
local v24 = 2*s3*r3 - 2*s2*r2
 
local w12 = v12 / v11
local w13 = v13 / v11
local w14 = v14 / v11
 
local w22 = v22 / v21 - w12
local w23 = v23 / v21 - w13
local w24 = v24 / v21 - w14
 
local p = -w23 / w22
local q = w24 / w22
local m = -w12*p - w13
local n = w14 - w12*q
 
local a = n*n + q*q - 1
local b = 2*m*n - 2*n*x1 + 2*p*q - 2*q*y1 + 2*s1*r1
local c = x1*x1 + m*m - 2*m*x1 + p*p + y1*y1 - 2*p*y1 - r1*r1
 
local d = b*b - 4*a*c
local rs = (-b - math.sqrt(d)) / (2*a)
local xs = m + n*rs
local ys = p + q*rs
 
return xs, ys, rs
end
</syntaxhighlight>
 
<syntaxhighlight lang="lua">
-- Example usage
local x1, y1, r1 = 0, 0, 1
local x2, y2, r2 = 4, 0, 1
local x3, y3, r3 = 2, 4, 2
 
print(solveApollonius(
x1, y1, r1,
x2, y2, r2,
x3, y3, r3,
1, 1, 1
))
 
print(solveApollonius(
x1, y1, r1,
x2, y2, r2,
x3, y3, r3,
-1, -1, -1
))
--Output:
--2.0 2.1 3.9
--2.0 0.83333333333333 1.1666666666667
</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Apolonius[a1_,b1_,c1_,a2_,b2_,c2_,a3_,b3_,c3_,S1_,S2_ ,S3_ ]:=
Module[{x1=a1,y1=b1,r1=c1,x2=a2,y2=b2,r2=c2,x3=a3,y3=b3,r3=c3,s1=S1,s2=S2,s3=S3},
v11 = 2*x2 - 2*x1; v12 = 2*y2 - 2*y1;
v13 = x1^2 - x2^2 + y1^2 - y2^2 - r1^2 + r2^2;
v14 = 2*s2*r2 - 2*s1*r1;
 
v21 = 2*x3-2*x2 ; v22 = 2*y3 - 2*y2;
v23 = x2^2 - x3^2 + y2^2 - y3^2 - r2^2 + r3^2;
v24 = 2*s3*r3 - 2*s2*r2;
 
w12 = v12/v11; w13 = v13/v11; w14 = v14/v11;
 
w22 = v22/v21 - w12;
w23 = v23/v21 - w13;
w24 = v24/v21 - w14;
 
p = -w23/w22; q=w24/w22;
m = -w12*p - w13; n=w14 - w12*q;
 
a = n^2 + q^2-1;
b = 2*m*n - 2*n*x1 + 2*p*q - 2*q*y1 + 2*s1*r1;
c = x1^2+m^2 - 2*m*x1 + p^2+y1^2 - 2*p*y1 - r1^2;
 
d= b^2 - 4*a*c;
rs = (-b -Sqrt[d])/(2*a);
xs = m + n*rs; ys = p + q*rs;
Map[N,{xs, ys, rs} ]]</syntaxhighlight>
 
<pre>Apolonius[0,0,1,2,4,2,4,0,1,1,1,1]
->{2.,2.1,3.9}
 
Apolonius[0,0,1,2,4,2,4,0,1,-1,-1,-1]
->{2.,0.833333,1.16667}</pre>
 
public static void main(String[] args){
Circle c1 = new Circle(new double[]{0,0}, 1);
Circle c2 = new Circle(new double[]{4,0}, 1);
Circle c3 = new Circle(new double[]{2,4}, 2);
System.out.println(solveApollonius(c1,c2,c3,1,1,1)); //Expects "Circle[x=2.00,y=2.10,r=3.90]" (green circle in image)
System.out.println(solveApollonius(c1,c2,c3,-1,-1,-1));//Expects "Circle[x=2.00,y=0.83,r=1.17]" (red circle in image)
}
}</lang>
=={{header|MUMPS}}==
{{trans|Java}}
<langsyntaxhighlight MUMPSlang="mumps">APOLLONIUS(CIR1,CIR2,CIR3,S1,S2,S3)
;Circles are passed in as strings with three parts with a "^" separator in the order x^y^r
;The three circles are CIR1, CIR2, and CIR3
Line 221 ⟶ 2,449:
SET $PIECE(CIRR,"^",3)=RS
KILL X1,X2,X3,Y1,Y2,Y3,R1,R2,R3,RS,V11,V12,V13,V14,V21,V22,V23,V24,W12,W13,W14,W22,W23,W24,P,M,N,Q,A,B,C,D
QUIT CIRR</langsyntaxhighlight>
In use:<pre>
USER>WRITE C1
Line 234 ⟶ 2,462:
2^.833333333333333333^1.166666666666666667
</pre>
 
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang="nim">import math
 
type Circle = tuple[x, y, r: float]
 
proc solveApollonius(c1, c2, c3: Circle; s1, s2, s3: float): Circle =
let
v11 = 2*c2.x - 2*c1.x
v12 = 2*c2.y - 2*c1.y
v13 = c1.x*c1.x - c2.x*c2.x + c1.y*c1.y - c2.y*c2.y - c1.r*c1.r + c2.r*c2.r
v14 = 2*s2*c2.r - 2*s1*c1.r
 
v21 = 2*c3.x - 2*c2.x
v22 = 2*c3.y - 2*c2.y
v23 = c2.x*c2.x - c3.x*c3.x + c2.y*c2.y - c3.y*c3.y - c2.r*c2.r + c3.r*c3.r
v24 = 2*s3*c3.r - 2*s2*c2.r
 
w12 = v12/v11
w13 = v13/v11
w14 = v14/v11
 
w22 = v22/v21-w12
w23 = v23/v21-w13
w24 = v24/v21-w14
 
p = -w23/w22
q = w24/w22
m = -w12*p-w13
n = w14 - w12*q
 
a = n*n + q*q - 1
b = 2*m*n - 2*n*c1.x + 2*p*q - 2*q*c1.y + 2*s1*c1.r
c = c1.x*c1.x + m*m - 2*m*c1.x + p*p + c1.y*c1.y - 2*p*c1.y - c1.r*c1.r
 
d = b*b-4*a*c
rs = (-b-sqrt(d))/(2*a)
 
xs = m+n*rs
ys = p+q*rs
 
return (xs, ys, rs)
 
let
c1: Circle = (0.0, 0.0, 1.0)
c2: Circle = (4.0, 0.0, 1.0)
c3: Circle = (2.0, 4.0, 2.0)
 
echo solveApollonius(c1, c2, c3, 1.0, 1.0, 1.0)
echo solveApollonius(c1, c2, c3, -1.0, -1.0, -1.0)</syntaxhighlight>
Output:
<pre>(x: 2.0, y: 2.1, r: 3.9)
(x: 2.0, y: 0.8333333333333333, r: 1.166666666666667)</pre>
 
=={{header|OCaml}}==
{{trans|C}}
 
<langsyntaxhighlight lang="ocaml">type point = { x:float; y:float }
type circle = {
center: point;
Line 313 ⟶ 2,595:
let r2 = solve_apollonius c1 c2 c3 (-1.) (-1.) (-1.) in
print_circle r2;
;;</langsyntaxhighlight>
 
=={{header|Perl}}==
Using the module <code>Math::Cartesian::Product</code> to generate the values to allow iteration through all solutions.
{{trans|Raku}}
<syntaxhighlight lang="perl">use utf8;
use Math::Cartesian::Product;
 
package Circle;
 
sub new {
my ($class, $args) = @_;
my $self = {
x => $args->{x},
y => $args->{y},
r => $args->{r},
};
bless $self, $class;
}
 
sub show {
my ($self, $args) = @_;
sprintf "x =%7.3f y =%7.3f r =%7.3f\n", $args->{x}, $args->{y}, $args->{r};
}
 
package main;
 
sub circle {
my($x,$y,$r) = @_;
Circle->new({ x => $x, y=> $y, r => $r });
}
 
sub solve_Apollonius {
my($c1, $c2, $c3, $s1, $s2, $s3) = @_;
 
my $𝑣11 = 2 * $c2->{x} - 2 * $c1->{x};
my $𝑣12 = 2 * $c2->{y} - 2 * $c1->{y};
my $𝑣13 = $c1->{x}**2 - $c2->{x}**2 + $c1->{y}**2 - $c2->{y}**2 - $c1->{r}**2 + $c2->{r}**2;
my $𝑣14 = 2 * $s2 * $c2->{r} - 2 * $s1 * $c1->{r};
 
my $𝑣21 = 2 * $c3->{x} - 2 * $c2->{x};
my $𝑣22 = 2 * $c3->{y} - 2 * $c2->{y};
my $𝑣23 = $c2->{x}**2 - $c3->{x}**2 + $c2->{y}**2 - $c3->{y}**2 - $c2->{r}**2 + $c3->{r}**2;
my $𝑣24 = 2 * $s3 * $c3->{r} - 2 * $s2 * $c2->{r};
 
my $𝑤12 = $𝑣12 / $𝑣11;
my $𝑤13 = $𝑣13 / $𝑣11;
my $𝑤14 = $𝑣14 / $𝑣11;
 
my $𝑤22 = $𝑣22 / $𝑣21 - $𝑤12;
my $𝑤23 = $𝑣23 / $𝑣21 - $𝑤13;
my $𝑤24 = $𝑣24 / $𝑣21 - $𝑤14;
 
my $𝑃 = -$𝑤23 / $𝑤22;
my $𝑄 = $𝑤24 / $𝑤22;
my $𝑀 = -$𝑤12 * $𝑃 - $𝑤13;
my $𝑁 = $𝑤14 - $𝑤12 * $𝑄;
 
my $𝑎 = $𝑁**2 + $𝑄**2 - 1;
my $𝑏 = 2 * $𝑀 * $𝑁 - 2 * $𝑁 * $c1->{x} + 2 * $𝑃 * $𝑄 - 2 * $𝑄 * $c1->{y} + 2 * $s1 * $c1->{r};
my $𝑐 = $c1->{x}**2 + $𝑀**2 - 2 * $𝑀 * $c1->{x} + $𝑃**2 + $c1->{y}**2 - 2 * $𝑃 * $c1->{y} - $c1->{r}**2;
 
my $𝐷 = $𝑏**2 - 4 * $𝑎 * $𝑐;
my $rs = (-$𝑏 - sqrt $𝐷) / (2 * $𝑎);
 
my $xs = $𝑀 + $𝑁 * $rs;
my $ys = $𝑃 + $𝑄 * $rs;
 
circle($xs, $ys, $rs);
}
 
$c1 = circle(0, 0, 1);
$c2 = circle(4, 0, 1);
$c3 = circle(2, 4, 2);
 
for (cartesian {@_} ([-1,1])x3) {
print Circle->show( solve_Apollonius $c1, $c2, $c3, @$_);
}</syntaxhighlight>
{{out}}
<pre>x = 2.000 y = 0.833 r = 1.167
x = 2.000 y = 3.214 r = 2.786
x = 3.002 y = 0.123 r = 2.005
x = 4.127 y = 3.252 r = 4.255
x = 0.998 y = 0.123 r = 2.005
x = -0.127 y = 3.252 r = 4.255
x = 2.000 y = -1.500 r = 3.500
x = 2.000 y = 2.100 r = 3.900</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">Apollonius</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">calc</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">circles</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s3</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">calc</span>
<span style="color: #004080;">atom</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r1</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">circles</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r2</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">circles</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">x3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r3</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">circles</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">v11</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x2</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">v12</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">y2</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">y1</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">v13</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x1</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">x2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x2</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">y1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">y1</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">y2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">y2</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">r1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r1</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">r2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r2</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">v14</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r2</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r1</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">v21</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x3</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">v22</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">y3</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">y2</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">v23</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x2</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">x3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x3</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">y2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">y2</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">y3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">y3</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">r2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r2</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">r3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r3</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">v24</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">s3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r3</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r2</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">w12</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">v12</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">v11</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">w13</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">v13</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">v11</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">w14</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">v14</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">v11</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">w22</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">v22</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">v21</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">w12</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">w23</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">v23</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">v21</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">w13</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">w24</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">v24</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">v21</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">w14</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">P</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">w23</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">w22</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">Q</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">w24</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">w22</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">M</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">w12</span><span style="color: #0000FF;">*</span><span style="color: #000000;">P</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">w13</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">N</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">w14</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">w12</span><span style="color: #0000FF;">*</span><span style="color: #000000;">Q</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">*</span><span style="color: #000000;">N</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">Q</span><span style="color: #0000FF;">*</span><span style="color: #000000;">Q</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">M</span><span style="color: #0000FF;">*</span><span style="color: #000000;">N</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">N</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x1</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">P</span><span style="color: #0000FF;">*</span><span style="color: #000000;">Q</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">Q</span><span style="color: #0000FF;">*</span><span style="color: #000000;">y1</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r1</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x1</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">M</span><span style="color: #0000FF;">*</span><span style="color: #000000;">M</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">M</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x1</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">P</span><span style="color: #0000FF;">*</span><span style="color: #000000;">P</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">y1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">y1</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">P</span><span style="color: #0000FF;">*</span><span style="color: #000000;">y1</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">r1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">r1</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">*</span><span style="color: #000000;">b</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">*</span><span style="color: #000000;">a</span><span style="color: #0000FF;">*</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">rs</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(-</span><span style="color: #000000;">b</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">))</span> <span style="color: #0000FF;">/</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">xs</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">M</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">*</span><span style="color: #000000;">rs</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">ys</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">P</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">Q</span><span style="color: #0000FF;">*</span><span style="color: #000000;">rs</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">xs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ys</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rs</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">circles</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}}</span>
<span style="color: #000080;font-style:italic;">-- +1: externally tangental, -1: internally tangental</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">calcs</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">8</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">atom</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">xs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ys</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rs</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">Apollonius</span><span style="color: #0000FF;">(</span><span style="color: #000000;">calcs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">circles</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">th</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"st (external)"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"nd (internal)"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"rd"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"th"</span><span style="color: #0000FF;">}[</span><span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)]</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d%s solution: x=%+f, y=%+f, r=%f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">th</span><span style="color: #0000FF;">,</span><span style="color: #000000;">xs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ys</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rs</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
1st (external) solution: x=+2.000000, y=+2.100000, r=3.900000
2nd (internal) solution: x=+2.000000, y=+0.833333, r=1.166667
3rd solution: x=+0.997502, y=+0.122502, r=2.004996
4th solution: x=+3.002498, y=+0.122502, r=2.004996
5th solution: x=+2.000000, y=+3.214286, r=2.785714
6th solution: x=+2.000000, y=-1.500000, r=3.500000
7th solution: x=+4.127498, y=+3.252498, r=4.254996
8th solution: x=-0.127498, y=+3.252498, r=4.254996
</pre>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">Apollonius: procedure options (main); /* 29 October 2013 */
 
define structure
1 circle,
2 x float (15),
2 y float (15),
2 radius float (15);
declare (c1 , c2, c3, result) type (circle);
 
c1.x = 0; c1.y = 0; c1.radius = 1;
c2.x = 4; c2.y = 0; c2.radius = 1;
c3.x = 2; c3.y = 4; c3.radius = 2;
 
result = Solve_Apollonius(c1, c2, c3, 1, 1, 1);
put skip edit ('External tangent:', result.x, result.y, result.radius) (a, 3 f(12,8));
 
result = Solve_Apollonius(c1, c2, c3, -1, -1, -1);
put skip edit ('Internal tangent:', result.x, result.y, result.radius) (a, 3 f(12,8));
 
 
Solve_Apollonius: procedure (c1, c2, c3, s1, s2, s3) returns(type(circle));
declare (c1, c2, c3) type(circle);
declare res type (circle);
declare (s1, s2, s3) fixed binary;
declare (
v11, v12, v13, v14,
v21, v22, v23, v24,
w12, w13, w14,
w22, w23, w24,
p, q, m, n, a, b, c, det) float (15);
v11 = 2*c2.x - 2*c1.x;
v12 = 2*c2.y - 2*c1.y;
v13 = c1.x**2 - c2.x**2 + c1.y**2 - c2.y**2 - c1.radius**2 + c2.radius**2;
v14 = 2*s2*c2.radius - 2*s1*c1.radius;
v21 = 2*c3.x - 2*c2.x;
v22 = 2*c3.y - 2*c2.y;
v23 = c2.x**2 - c3.x**2 + c2.y**2 - c3.y**2 - c2.radius**2 + c3.radius**2;
v24 = 2*s3*c3.radius - 2*s2*c2.radius;
w12 = v12/v11;
w13 = v13/v11;
w14 = v14/v11;
w22 = v22/v21-w12;
w23 = v23/v21-w13;
w24 = v24/v21-w14;
p = -w23/w22;
q = w24/w22;
m = -w12*P - w13;
n = w14 - w12*q;
a = n*n + q*q - 1;
b = 2*m*n - 2*n*c1.x + 2*p*q - 2*q*c1.y + 2*s1*c1.radius;
c = c1.x**2 + m*m - 2*m*c1.x + p*p + c1.y**2 - 2*p*c1.y - c1.radius**2;
det = b*b - 4*a*c;
res.radius = (-b-sqrt(det)) / (2*a);
res.x = m + n*res.radius;
res.y = p + q*res.radius;
 
return (res);
end Solve_Apollonius;
end Apollonius;</syntaxhighlight>
Results:
<pre>
External tangent: 2.00000000 2.10000000 3.90000000
Internal tangent: 2.00000000 0.83333333 1.16666667</pre>
 
=={{header|PowerShell}}==
{{trans|C#}}
<syntaxhighlight lang="powershell">
function Measure-Apollonius
{
[CmdletBinding()]
[OutputType([PSCustomObject])]
Param
(
[int]$Counter,
[double]$x1,
[double]$y1,
[double]$r1,
[double]$x2,
[double]$y2,
[double]$r2,
[double]$x3,
[double]$y3,
[double]$r3
)
 
switch ($Counter)
{
{$_ -eq 2} {$s1 = -1; $s2 = -1; $s3 = -1; break}
{$_ -eq 3} {$s1 = 1; $s2 = -1; $s3 = -1; break}
{$_ -eq 4} {$s1 = -1; $s2 = 1; $s3 = -1; break}
{$_ -eq 5} {$s1 = -1; $s2 = -1; $s3 = 1; break}
{$_ -eq 6} {$s1 = 1; $s2 = 1; $s3 = -1; break}
{$_ -eq 7} {$s1 = -1; $s2 = 1; $s3 = 1; break}
{$_ -eq 8} {$s1 = 1; $s2 = -1; $s3 = 1; break}
Default {$s1 = 1; $s2 = 1; $s3 = 1; break}
}
 
[double]$v11 = 2 * $x2 - 2 * $x1
[double]$v12 = 2 * $y2 - 2 * $y1
[double]$v13 = $x1 * $x1 - $x2 * $x2 + $y1 * $y1 - $y2 * $y2 - $r1 * $r1 + $r2 * $r2
[double]$v14 = 2 * $s2 * $r2 - 2 * $s1 * $r1
[double]$v21 = 2 * $x3 - 2 * $x2
[double]$v22 = 2 * $y3 - 2 * $y2
[double]$v23 = $x2 * $x2 - $x3 * $x3 + $y2 * $y2 - $y3 * $y3 - $r2 * $r2 + $r3 * $r3
[double]$v24 = 2 * $s3 * $r3 - 2 * $s2 * $r2
[double]$w12 = $v12 / $v11
[double]$w13 = $v13 / $v11
[double]$w14 = $v14 / $v11
[double]$w22 = $v22 / $v21 - $w12
[double]$w23 = $v23 / $v21 - $w13
[double]$w24 = $v24 / $v21 - $w14
[double]$P = -$w23 / $w22
[double]$Q = $w24 / $w22
[double]$M = -$w12 * $P - $w13
[double]$N = $w14 - $w12 * $Q
 
[double]$a = $N * $N + $Q * $Q - 1
[double]$b = 2 * $M * $N - 2 * $N * $x1 + 2 * $P * $Q - 2 * $Q * $y1 + 2 * $s1 * $r1
[double]$c = $x1 * $x1 + $M * $M - 2 * $M * $x1 + $P * $P + $y1 * $y1 - 2 * $P * $y1 - $r1 * $r1
[double]$D = $b * $b - 4 * $a * $c
[double]$rs = (-$b - [Double]::Parse([Math]::Sqrt($D).ToString())) / (2 * [Double]::Parse($a.ToString()))
[double]$xs = $M + $N * $rs
[double]$ys = $P + $Q * $rs
 
[PSCustomObject]@{
X = $xs
Y = $ys
Radius = $rs
}
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
for ($i = 1; $i -le 8; $i++)
{
Measure-Apollonius -Counter $i -x1 0 -y1 0 -r1 1 -x2 4 -y2 0 -r2 1 -x3 2 -y3 4 -r3 2
}
</syntaxhighlight>
{{Out}}
<pre>
X Y Radius
- - ------
2 2.1 3.9
2 0.833333333333333 1.16666666666667
0.997501996806385 0.122501996806385 2.00499600638723
3.00249800319362 0.122501996806385 2.00499600638723
2 3.21428571428571 2.78571428571429
2 -1.5 3.5
4.12749800319362 3.25249800319362 4.25499600638723
-0.127498003193615 3.25249800319362 4.25499600638723
</pre>
 
=={{header|PureBasic}}==
{{trans|Java}}
<langsyntaxhighlight PureBasiclang="purebasic">Structure Circle
XPos.f
YPos.f
Line 391 ⟶ 3,004:
EndIf
Print("Press ENTER to exit"): Input()
EndIf</langsyntaxhighlight>
<pre>Circle [x=2.00, y=2.10, r=3.90]
Circle [x=2.00, y=0.83, r=1.17]
Line 398 ⟶ 3,011:
=={{header|Python}}==
{{trans|Java}}. Although a Circle class is defined, the solveApollonius function is defined in such a way that any three valued tuple or list could be used instead of c1, c2, and c3. The function calls near the end use instances of the Circle class, whereas the docstring shows how the same can be achieved using simple tuples. (And also serves as a simple [[wp:Doctest|doctest]])
<langsyntaxhighlight lang="python">
from collections import namedtuple
import math
Line 454 ⟶ 3,067:
c1, c2, c3 = Circle(0, 0, 1), Circle(4, 0, 1), Circle(2, 4, 2)
print(solveApollonius(c1, c2, c3, 1, 1, 1)) #Expects "Circle[x=2.00,y=2.10,r=3.90]" (green circle in image)
print(solveApollonius(c1, c2, c3, -1, -1, -1)) #Expects "Circle[x=2.00,y=0.83,r=1.17]" (red circle in image)</langsyntaxhighlight>
'''Sample Output'''
<pre>Circle(x=2.0, y=2.1, r=3.9)
Circle(x=2.0, y=0.8333333333333333, r=1.1666666666666667)</pre>
 
=={{header|Racket}}==
{{trans|Java}}
<syntaxhighlight lang="racket">
#lang slideshow
 
(struct circle (x y r) #:prefab)
 
(define (apollonius c1 c2 c3 s1 s2 s3)
(define x1 (circle-x c1))
(define y1 (circle-y c1))
(define r1 (circle-r c1))
(define x2 (circle-x c2))
(define y2 (circle-y c2))
(define r2 (circle-r c2))
(define x3 (circle-x c3))
(define y3 (circle-y c3))
(define r3 (circle-r c3))
 
(define v11 (- (* 2 x2) (* 2 x1)))
(define v12 (- (* 2 y2) (* 2 y1)))
(define v13 (+ (- (* x1 x1) (* x2 x2))
(- (* y1 y1) (* y2 y2))
(- (* r2 r2) (* r1 r1))))
(define v14 (- (* 2 s2 r2) (* 2 s1 r1)))
 
(define v21 (- (* 2 x3) (* 2 x2)))
(define v22 (- (* 2 y3) (* 2 y2)))
(define v23 (+ (- (* x2 x2) (* x3 x3))
(- (* y2 y2) (* y3 y3))
(- (* r3 r3) (* r2 r2))))
(define v24 (- (* 2 s3 r3) (* 2 s2 r2)))
 
(define w12 (/ v12 v11))
(define w13 (/ v13 v11))
(define w14 (/ v14 v11))
 
(define w22 (- (/ v22 v21) w12))
(define w23 (- (/ v23 v21) w13))
(define w24 (- (/ v24 v21) w14))
 
(define P (- (/ w23 w22)))
(define Q (/ w24 w22))
(define M (- (+ (* w12 P) w13)))
(define N (- w14 (* w12 Q)))
 
(define a (+ (* N N) (* Q Q) -1))
(define b (+ (- (* 2 M N) (* 2 N x1))
(- (* 2 P Q) (* 2 Q y1))
(* 2 s1 r1)))
(define c (- (+ (* x1 x1) (* M M) (* P P) (* y1 y1))
(+ (* 2 M x1) (* 2 P y1) (* r1 r1))))
 
(define D (- (* b b) (* 4 a c)))
(define rs (/ (- (+ b (sqrt D))) (* 2 a)))
(define xs (+ M (* N rs)))
(define ys (+ P (* Q rs)))
(circle xs ys rs))
 
(define c1 (circle 0.0 0.0 1.0))
(define c2 (circle 4.0 0.0 1.0))
(define c3 (circle 2.0 4.0 2.0))
 
;; print solutions
(apollonius c1 c2 c3 1.0 1.0 1.0)
(apollonius c1 c2 c3 -1.0 -1.0 -1.0)
 
;; visualize solutions
(require racket/gui/base)
(define (show-circles . circles+colors)
(define f (new frame% [label "Apollonius"] [width 300] [height 300]))
(define c
(new canvas% [parent f]
[paint-callback
(lambda (canvas dc)
(send* dc (set-origin 100 100)
(set-scale 20 20)
(set-pen "black" 1/10 'solid)
(set-brush "white" 'transparent))
(for ([x circles+colors])
(if (string? x)
(send dc set-pen x 1/5 'solid)
(let ([x (circle-x x)] [y (circle-y x)] [r (circle-r x)])
(send dc draw-ellipse (- x r) (- y r) (* 2 r) (* 2 r))))))]))
(send f show #t))
(show-circles "black" c1 c2 c3
"green" (apollonius c1 c2 c3 1.0 1.0 1.0)
"red" (apollonius c1 c2 c3 -1.0 -1.0 -1.0))
</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
This program is written mostly in the "sigilless" style for several reasons. First, sigils tend to imply variables, and these sigilless symbols are not variables, but readonly bindings to values that are calculated only once, so leaving off the sigil emphasizes the fact that they are not variables, but merely named intermediate results.
 
Second, it looks more like the original mathematical formulas to do it this way.
 
Third, together with the use of Unicode, we are emphasizing the social contract between the writer and the reader, which has a clause in it that indicates code is read much more often than it is written, therefore the writer is obligated to undergo vicarious suffering on behalf of the reader to make things clear. If the reader doesn't understand, it's the writer's fault, in other words. Or in other other words, figure out how to type those Unicode characters, even if it's hard. And you should type them whenever it makes things clearer to the reader.
 
Finally, writing in an [https://en.wikipedia.org/wiki/Static_single_assignment_form SSA style] tends to help the optimizer.
 
<syntaxhighlight lang="raku" line>class Circle {
has $.x;
has $.y;
has $.r;
method gist { sprintf "%s =%7.3f " xx 3, (:$!x,:$!y,:$!r)».kv }
}
 
sub circle($x,$y,$r) { Circle.new: :$x, :$y, :$r }
 
sub solve-Apollonius([\c1, \c2, \c3], [\s1, \s2, \s3]) {
my \𝑣11 = 2 * c2.x - 2 * c1.x;
my \𝑣12 = 2 * c2.y - 2 * c1.y;
my \𝑣13 = c1.x² - c2.x² + c1.y² - c2.y² - c1.r² + c2.r²;
my \𝑣14 = 2 * s2 * c2.r - 2 * s1 * c1.r;
my \𝑣21 = 2 * c3.x - 2 * c2.x;
my \𝑣22 = 2 * c3.y - 2 * c2.y;
my \𝑣23 = c2.x² - c3.x² + c2.y² - c3.y² - c2.r² + c3.r²;
my \𝑣24 = 2 * s3 * c3.r - 2 * s2 * c2.r;
my \𝑤12 = 𝑣12 / 𝑣11;
my \𝑤13 = 𝑣13 / 𝑣11;
my \𝑤14 = 𝑣14 / 𝑣11;
my \𝑤22 = 𝑣22 / 𝑣21 - 𝑤12;
my \𝑤23 = 𝑣23 / 𝑣21 - 𝑤13;
my \𝑤24 = 𝑣24 / 𝑣21 - 𝑤14;
my \𝑃 = -𝑤23 / 𝑤22;
my \𝑄 = 𝑤24 / 𝑤22;
my \𝑀 = -𝑤12 * 𝑃 - 𝑤13;
my \𝑁 = 𝑤14 - 𝑤12 * 𝑄;
my \𝑎 = 𝑁² + 𝑄² - 1;
my \𝑏 = 2 * 𝑀 * 𝑁 - 2 * 𝑁 * c1.x + 2 * 𝑃 * 𝑄 - 2 * 𝑄 * c1.y + 2 * s1 * c1.r;
my \𝑐 = c1.x² + 𝑀² - 2 * 𝑀 * c1.x + 𝑃² + c1.y² - 2 * 𝑃 * c1.y - c1.r²;
my \𝐷 = 𝑏² - 4 * 𝑎 * 𝑐;
my \rs = (-𝑏 - sqrt 𝐷) / (2 * 𝑎);
my \xs = 𝑀 + 𝑁 * rs;
my \ys = 𝑃 + 𝑄 * rs;
circle(xs, ys, rs);
}
my @c = circle(0, 0, 1), circle(4, 0, 1), circle(2, 4, 2);
for ([X] [-1,1] xx 3) -> @i {
say (solve-Apollonius @c, @i).gist;
}</syntaxhighlight>
{{out}}
<pre>x = 2.000 y = 0.833 r = 1.167
x = 2.000 y = 3.214 r = 2.786
x = 3.002 y = 0.123 r = 2.005
x = 4.127 y = 3.252 r = 4.255
x = 0.998 y = 0.123 r = 2.005
x = -0.127 y = 3.252 r = 4.255
x = 2.000 y = -1.500 r = 3.500
x = 2.000 y = 2.100 r = 3.900</pre>
 
=={{header|REXX}}==
Programming note: &nbsp; REXX has no &nbsp; '''sqrt''' &nbsp; (square root) function, so a RYO version is included here.
<syntaxhighlight lang="rexx">/*REXX program solves the problem of Apollonius, named after the Greek Apollonius of */
/*────────────────────────────────────── Perga [Pergæus] (circa 262 BCE ──► 190 BCE). */
numeric digits 15; x1= 0; y1= 0; r1= 1
x2= 4; y2= 0; r2= 1
x3= 2; y3= 4; r3= 2
call tell 'external tangent: ', Apollonius( 1, 1, 1)
call tell 'internal tangent: ', Apollonius(-1, -1, -1)
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Apollonius: parse arg s1,s2,s3 /*could be internal or external tangent*/
numeric digits digits() * 3 /*reduce rounding with thrice digits. */
va= x2*2 - x1*2; vb= y2*2 - y1*2
vc= x1**2 - x2**2 + y1**2 - y2**2 - r1**2 + r2**2
vd= s2*r2*2 - s1*r1*2; ve= x3*2 - x2*2; vf= y3*2 - y2*2
vg= x2**2 - x3**2 + y2**2 - y3**2 - r2**2 + r3**2; vh= s3*r3*2 - s2*r2*2
vj= vb/va; vk= vc/va; vm= vd/va; vn= vf/ve - vj
vp= vg/ve - vk; vr= vh/ve - vm; p = -vp/vn; q = vr/vn
m = -vj*p - vk; n = vm - vj*q
a = n**2 + q**2 - 1
b = (m*n - n*x1 + p*q - q*y1 + s1*r1) * 2
c = x1**2 + y1**2 + m**2 - r1**2 + p**2 - (m*x1 + p*y1) * 2
$r= (-b - sqrt(b**2 - a*c*4) ) / (a+a)
return (m + n*$r) (p + q*$r) ($r) /*return 3 arguments.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); h=d+6; numeric digits
m.=9; numeric form; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_%2
do j=0 while h>9; m.j=h; h= h % 2 + 1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g) * .5; end /*k*/; return g
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell: parse arg _,a b c; w=digits()+4; say _ left(a/1,w%2) left(b/1,w) left(c/1,w); return</syntaxhighlight>
Programming note: &nbsp; in REXX, dividing by unity normalizes the number.
{{out|output|text=&nbsp; when using the default input:}}
<pre>
external tangent: 2 2.1 3.9
internal tangent: 2 0.833333333333333 1.16666666666667
</pre>
 
=={{header|Ruby}}==
{{trans|Java}}
 
<syntaxhighlight lang="ruby">class Circle
def initialize(x, y, r)
@x, @y, @r = [x, y, r].map(&:to_f)
end
attr_reader :x, :y, :r
def self.apollonius(c1, c2, c3, s1=1, s2=1, s3=1)
x1, y1, r1 = c1.x, c1.y, c1.r
x2, y2, r2 = c2.x, c2.y, c2.r
x3, y3, r3 = c3.x, c3.y, c3.r
v11 = 2*x2 - 2*x1
v12 = 2*y2 - 2*y1
v13 = x1**2 - x2**2 + y1**2 - y2**2 - r1**2 + r2**2
v14 = 2*s2*r2 - 2*s1*r1
v21 = 2*x3 - 2*x2
v22 = 2*y3 - 2*y2
v23 = x2**2 - x3**2 + y2**2 - y3**2 - r2**2 + r3**2
v24 = 2*s3*r3 - 2*s2*r2
w12 = v12/v11
w13 = v13/v11
w14 = v14/v11
w22 = v22/v21 - w12
w23 = v23/v21 - w13
w24 = v24/v21 - w14
p = -w23/w22
q = w24/w22
m = -w12*p - w13
n = w14 - w12*q
a = n**2 + q**2 - 1
b = 2*m*n - 2*n*x1 + 2*p*q - 2*q*y1 + 2*s1*r1
c = x1**2 + m**2 - 2*m*x1 + p**2 + y1**2 - 2*p*y1 - r1**2
d = b**2 - 4*a*c
rs = (-b - Math.sqrt(d)) / (2*a)
xs = m + n*rs
ys = p + q*rs
self.new(xs, ys, rs)
end
def to_s
"Circle: x=#{@x}, y=#{@y}, r=#{@r}"
end
end
 
puts c1 = Circle.new(0, 0, 1)
puts c2 = Circle.new(2, 4, 2)
puts c3 = Circle.new(4, 0, 1)
 
puts Circle.apollonius(c1, c2, c3)
puts Circle.apollonius(c1, c2, c3, -1, -1, -1)</syntaxhighlight>
 
{{out}}
<pre>
Circle: x=0.0, y=0.0, r=1.0
Circle: x=2.0, y=4.0, r=2.0
Circle: x=4.0, y=0.0, r=1.0
Circle: x=2.0, y=2.1, r=3.9
Circle: x=2.0, y=0.8333333333333333, r=1.1666666666666667
</pre>
 
=={{header|Scala}}==
<syntaxhighlight lang="scala">object ApolloniusSolver extends App {
case class Circle(x: Double, y: Double, r: Double)
object Tangent extends Enumeration {
type Tangent = Value
val intern = Value(-1)
val extern = Value(1)
}
 
import Tangent._
import scala.Math._
 
val solveApollonius: (Circle, Circle, Circle, Triple[Tangent, Tangent, Tangent]) => Circle = (c1, c2, c3, tangents) => {
val fv: (Circle, Circle, Int, Int) => Tuple4[Double, Double, Double, Double] = (c1, c2, s1, s2) => {
val v11 = 2 * c2.x - 2 * c1.x
val v12 = 2 * c2.y - 2 * c1.y
val v13 = pow(c1.x, 2) - pow(c2.x, 2) + pow(c1.y, 2) - pow(c2.y, 2) - pow(c1.r, 2) + pow(c2.r, 2)
val v14 = 2 * s2 * c2.r - 2 * s1 * c1.r
Tuple4(v11, v12, v13, v14)
}
val (s1, s2, s3) = (tangents._1.id, tangents._2.id, tangents._3.id)
 
val (v11, v12, v13, v14) = fv(c1, c2, s1, s2)
val (v21, v22, v23, v24) = fv(c2, c3, s2, s3)
 
val w12 = v12 / v11
val w13 = v13 / v11
val w14 = v14 / v11
val w22 = v22 / v21 - w12
val w23 = v23 / v21 - w13
val w24 = v24 / v21 - w14
val P = -w23 / w22
val Q = w24 / w22
val M = -w12 * P - w13
val N = w14 - w12 * Q
val a = N*N + Q*Q - 1
val b = 2*M*N - 2*N*c1.x +
2*P*Q - 2*Q*c1.y +
2*s1*c1.r
val c = pow(c1.x, 2) + M*M - 2*M*c1.x +
P*P + pow(c1.y, 2) - 2*P*c1.y - pow(c1.r, 2)
// Find a root of a quadratic equation. This requires the circle centers not to be e.g. colinear
val D = b*b - 4*a*c
val rs = (-b - sqrt(D)) / (2*a)
Circle(x=M + N*rs, y=P + Q*rs, r=rs)
}
val c1 = Circle(x=0.0, y=0.0, r=1.0)
val c2 = Circle(x=4.0, y=0.0, r=1.0)
val c3 = Circle(x=2.0, y=4.0, r=2.0)
 
println("c1: "+c1)
println("c2: "+c2)
println("c3: "+c3)
println{
val tangents = Triple(intern, intern, intern)
"red circle: tangents="+tangents+" cs=" + solveApollonius(c1, c2, c3, tangents)
}
println{
val tangents = Triple(extern, extern, extern)
"green circle: tangents="+tangents+" cs=" + solveApollonius(c1, c2, c3, tangents)
}
 
println("all combinations:")
for ( ti <- Tangent.values)
for ( tj <- Tangent.values)
for ( tk <- Tangent.values) {
println{
val format: Circle => String = c => {
"Circle(x=%8.5f, y=%8.5f, r=%8.5f)".format(c.x, c.y, c.r)
}
val tangents = Triple(ti, tj, tk)
"tangents: " + tangents + " -> cs=" + format(solveApollonius(c1, c2, c3, tangents))
}
}
}</syntaxhighlight>
Output:
<pre>
c1: Circle(0.0,0.0,1.0)
c2: Circle(4.0,0.0,1.0)
c3: Circle(2.0,4.0,2.0)
red circle: tangents=(intern,intern,intern) cs=Circle(2.0,0.8333333333333333,1.1666666666666667)
green circle: tangents=(extern,extern,extern) cs=Circle(2.0,2.1,3.9)
all combinations:
tangents: (intern,intern,intern) -> cs=Circle(x= 2,00000, y= 0,83333, r= 1,16667)
tangents: (intern,intern,extern) -> cs=Circle(x= 2,00000, y= 3,21429, r= 2,78571)
tangents: (intern,extern,intern) -> cs=Circle(x= 3,00250, y= 0,12250, r= 2,00500)
tangents: (intern,extern,extern) -> cs=Circle(x= 4,12750, y= 3,25250, r= 4,25500)
tangents: (extern,intern,intern) -> cs=Circle(x= 0,99750, y= 0,12250, r= 2,00500)
tangents: (extern,intern,extern) -> cs=Circle(x=-0,12750, y= 3,25250, r= 4,25500)
tangents: (extern,extern,intern) -> cs=Circle(x= 2,00000, y=-1,50000, r= 3,50000)
tangents: (extern,extern,extern) -> cs=Circle(x= 2,00000, y= 2,10000, r= 3,90000)
</pre>
 
=={{header|Sidef}}==
{{trans|Raku}}
<syntaxhighlight lang="ruby">class Circle(x,y,r) {
method to_s { "Circle(#{x}, #{y}, #{r})" }
}
 
func solve_apollonius(c, s) {
 
var(c1, c2, c3) = c...;
var(s1, s2, s3) = s...;
 
var 𝑣11 = (2*c2.x - 2*c1.x);
var 𝑣12 = (2*c2.y - 2*c1.y);
var 𝑣13 = (c1.x**2 - c2.x**2 + c1.y**2 - c2.y**2 - c1.r**2 + c2.r**2);
var 𝑣14 = (2*s2*c2.r - 2*s1*c1.r);
 
var 𝑣21 = (2*c3.x - 2*c2.x);
var 𝑣22 = (2*c3.y - 2*c2.y);
var 𝑣23 = (c2.x**2 - c3.x**2 + c2.y**2 - c3.y**2 - c2.r**2 + c3.r**2);
var 𝑣24 = (2*s3*c3.r - 2*s2*c2.r);
 
var 𝑤12 = (𝑣12 / 𝑣11);
var 𝑤13 = (𝑣13 / 𝑣11);
var 𝑤14 = (𝑣14 / 𝑣11);
 
var 𝑤22 = (𝑣22/𝑣21 - 𝑤12);
var 𝑤23 = (𝑣23/𝑣21 - 𝑤13);
var 𝑤24 = (𝑣24/𝑣21 - 𝑤14);
 
var 𝑃 = (-𝑤23 / 𝑤22);
var 𝑄 = (𝑤24 / 𝑤22);
var 𝑀 = (-𝑤12*𝑃 - 𝑤13);
var 𝑁 = (𝑤14 - 𝑤12*𝑄);
 
var 𝑎 = (𝑁**2 + 𝑄**2 - 1);
var 𝑏 = (2*𝑀*𝑁 - 2*𝑁*c1.x + 2*𝑃*𝑄 - 2*𝑄*c1.y + 2*s1*c1.r);
var 𝑐 = (c1.x**2 + 𝑀**2 - 2*𝑀*c1.x + 𝑃**2 + c1.y**2 - 2*𝑃*c1.y - c1.r**2);
 
var 𝐷 = (𝑏**2 - 4*𝑎*𝑐);
var rs = ((-𝑏 - 𝐷.sqrt) / 2*𝑎);
 
var xs = (𝑀 + 𝑁*rs);
var ys = (𝑃 + 𝑄*rs);
 
Circle(xs, ys, rs);
}
 
var c = [Circle(0, 0, 1), Circle(4, 0, 1), Circle(2, 4, 2)];
say solve_apollonius(c, %n<1 1 1>);
say solve_apollonius(c, %n<-1 -1 -1>);</syntaxhighlight>
{{out}}
<pre>
Circle(2, 2.1, 3.9)
Circle(2, 0.83333333333333333333333333333333333333325, 1.166666666666666666666666666666666666667)
</pre>
 
=={{header|Swift}}==
{{trans|Java}}
<syntaxhighlight lang="swift">import Foundation
 
struct Circle {
let center:[Double]!
let radius:Double!
init(center:[Double], radius:Double) {
self.center = center
self.radius = radius
}
func toString() -> String {
return "Circle[x=\(center[0]),y=\(center[1]),r=\(radius)]"
}
}
 
func solveApollonius(c1:Circle, c2:Circle, c3:Circle,
s1:Double, s2:Double, s3:Double) -> Circle {
let x1 = c1.center[0]
let y1 = c1.center[1]
let r1 = c1.radius
let x2 = c2.center[0]
let y2 = c2.center[1]
let r2 = c2.radius
let x3 = c3.center[0]
let y3 = c3.center[1]
let r3 = c3.radius
let v11 = 2*x2 - 2*x1
let v12 = 2*y2 - 2*y1
let v13 = x1*x1 - x2*x2 + y1*y1 - y2*y2 - r1*r1 + r2*r2
let v14 = 2*s2*r2 - 2*s1*r1
let v21 = 2*x3 - 2*x2
let v22 = 2*y3 - 2*y2
let v23 = x2*x2 - x3*x3 + y2*y2 - y3*y3 - r2*r2 + r3*r3
let v24 = 2*s3*r3 - 2*s2*r2
let w12 = v12/v11
let w13 = v13/v11
let w14 = v14/v11
let w22 = v22/v21-w12
let w23 = v23/v21-w13
let w24 = v24/v21-w14
let P = -w23/w22
let Q = w24/w22
let M = -w12*P-w13
let N = w14 - w12*Q
let a = N*N + Q*Q - 1
let b = 2*M*N - 2*N*x1 + 2*P*Q - 2*Q*y1 + 2*s1*r1
let c = x1*x1 + M*M - 2*M*x1 + P*P + y1*y1 - 2*P*y1 - r1*r1
let D = b*b-4*a*c
let rs = (-b - sqrt(D)) / (2*a)
let xs = M + N * rs
let ys = P + Q * rs
return Circle(center: [xs,ys], radius: rs)
}
 
let c1 = Circle(center: [0,0], radius: 1)
let c2 = Circle(center: [4,0], radius: 1)
let c3 = Circle(center: [2,4], radius: 2)
 
println(solveApollonius(c1,c2,c3,1,1,1).toString())
println(solveApollonius(c1,c2,c3,-1,-1,-1).toString())</syntaxhighlight>
{{out}}
<pre>Circle[x=2.0,y=2.1,r=3.9]
Circle[x=2.0,y=0.833333333333333,r=1.16666666666667]</pre>
 
=={{header|Tcl}}==
{{trans|Java}}
{{works with|Tcl|8.6}} or {{libheader|TclOO}}
<lang tcl>package require TclOO; # Just so we can make a circle class
<syntaxhighlight lang="tcl">package require TclOO; # Just so we can make a circle class
 
oo::class create circle {
Line 516 ⟶ 3,633:
 
return [circle new $xs $ys $rs]
}</langsyntaxhighlight>
Demonstration code:
<langsyntaxhighlight lang="tcl">set c1 [circle new 0 0 1]
set c2 [circle new 4 0 1]
set c3 [circle new 2 4 2]
Line 524 ⟶ 3,641:
set sB [solveApollonius $c1 $c2 $c3 -1 -1 -1]
puts [$sA format]
puts [$sB format]</langsyntaxhighlight>
Output:
<pre>
Line 531 ⟶ 3,648:
</pre>
Note that the Tcl code uses the <code>**</code> (exponentiation) operator to shorten and simplify some operations, and that the <code>circle</code> class is forcing the interpretation of every circle's coordinates as double-precision floating-point numbers.
 
{{omit from|GUISS}}
 
=={{header|VBA}}==
 
<syntaxhighlight lang="vba/vbasic 6.0">
Option Explicit
Option Base 0
 
Private Const intBase As Integer = 0
 
Private Type tPoint
X As Double
Y As Double
End Type
Private Type tCircle
Centre As tPoint
Radius As Double
End Type
 
Private Sub sApollonius()
Dim Circle1 As tCircle
Dim Circle2 As tCircle
Dim Circle3 As tCircle
Dim CTanTanTan(intBase + 0 to intBase + 7) As tCircle
 
With Circle1
With .Centre
.X = 0
.Y = 0
End With
.Radius = 1
End With
 
With Circle2
With .Centre
.X = 4
.Y = 0
End With
.Radius = 1
End With
 
With Circle3
With .Centre
.X = 2
.Y = 4
End With
.Radius = 2
End With
 
Call fApollonius(Circle1,Circle2,Circle3,CTanTanTan()))
 
End Sub
 
Public Function fApollonius(ByRef C1 As tCircle, _
ByRef C2 As tCircle, _
ByRef C3 As tCircle, _
ByRef CTanTanTan() As tCircle) As Boolean
' Solves the Problem of Apollonius (finding a circle tangent to three other circles in the plane)
' (x_s - x_1)^2 + (y_s - y_1)^2 = (r_s - Tan_1 * r_1)^2
' (x_s - x_2)^2 + (y_s - y_2)^2 = (r_s - Tan_2 * r_2)^2
' (x_s - x_3)^2 + (y_s - y_3)^2 = (r_s - Tan_3 * r_3)^2
' x_s = M + N * r_s
' y_s = P + Q * r_s
 
' Parameters:
' C1, C2, C3 (circles in the problem)
' Tan1 := An indication if the solution should be externally or internally tangent (+1/-1) to Circle1 (C1)
' Tan2 := An indication if the solution should be externally or internally tangent (+1/-1) to Circle2 (C2)
' Tan3 := An indication if the solution should be externally or internally tangent (+1/-1) to Circle3 (C3)
 
Dim Tangent(intBase + 0 To intBase + 7, intBase + 0 To intBase + 2) As Integer
Dim lgTangent As Long
Dim Tan1 As Integer
Dim Tan2 As Integer
Dim Tan3 As Integer
Dim v11 As Double
Dim v12 As Double
Dim v13 As Double
Dim v14 As Double
Dim v21 As Double
Dim v22 As Double
Dim v23 As Double
Dim v24 As Double
Dim w12 As Double
Dim w13 As Double
Dim w14 As Double
Dim w22 As Double
Dim w23 As Double
Dim w24 As Double
Dim p As Double
Dim Q As Double
Dim M As Double
Dim N As Double
Dim A As Double
Dim b As Double
Dim c As Double
Dim D As Double
 
'Check if circle centers are colinear
If fColinearPoints(C1.Centre, C2.Centre, C3.Centre) Then
fApollonius = False
Exit Function
End If
Tangent(intBase + 0, intBase + 0) = -1
Tangent(intBase + 0, intBase + 1) = -1
Tangent(intBase + 0, intBase + 2) = -1
Tangent(intBase + 1, intBase + 0) = -1
Tangent(intBase + 1, intBase + 1) = -1
Tangent(intBase + 1, intBase + 2) = 1
Tangent(intBase + 2, intBase + 0) = -1
Tangent(intBase + 2, intBase + 1) = 1
Tangent(intBase + 2, intBase + 2) = -1
Tangent(intBase + 3, intBase + 0) = -1
Tangent(intBase + 3, intBase + 1) = 1
Tangent(intBase + 3, intBase + 2) = 1
Tangent(intBase + 4, intBase + 0) = 1
Tangent(intBase + 4, intBase + 1) = -1
Tangent(intBase + 4, intBase + 2) = -1
Tangent(intBase + 5, intBase + 0) = 1
Tangent(intBase + 5, intBase + 1) = -1
Tangent(intBase + 5, intBase + 2) = 1
Tangent(intBase + 6, intBase + 0) = 1
Tangent(intBase + 6, intBase + 1) = 1
Tangent(intBase + 6, intBase + 2) = -1
Tangent(intBase + 7, intBase + 0) = 1
Tangent(intBase + 7, intBase + 1) = 1
Tangent(intBase + 7, intBase + 2) = 1
For lgTangent = LBound(Tangent) To UBound(Tangent)
Tan1 = Tangent(lgTangent, intBase + 0)
Tan2 = Tangent(lgTangent, intBase + 1)
Tan3 = Tangent(lgTangent, intBase + 2)
 
v11 = 2 * (C2.Centre.X - C1.Centre.X)
v12 = 2 * (C2.Centre.Y - C1.Centre.Y)
v13 = (C1.Centre.X * C1.Centre.X) _
- (C2.Centre.X * C2.Centre.X) _
+ (C1.Centre.Y * C1.Centre.Y) _
- (C2.Centre.Y * C2.Centre.Y) _
- (C1.Radius * C1.Radius) _
+ (C2.Radius * C2.Radius)
v14 = 2 * (Tan2 * C2.Radius - Tan1 * C1.Radius)
v21 = 2 * (C3.Centre.X - C2.Centre.X)
v22 = 2 * (C3.Centre.Y - C2.Centre.Y)
v23 = (C2.Centre.X * C2.Centre.X) _
- (C3.Centre.X * C3.Centre.X) _
+ (C2.Centre.Y * C2.Centre.Y) _
- (C3.Centre.Y * C3.Centre.Y) _
- (C2.Radius * C2.Radius) _
+ (C3.Radius * C3.Radius)
v24 = 2 * ((Tan3 * C3.Radius) - (Tan2 * C2.Radius))
w12 = v12 / v11
w13 = v13 / v11
w14 = v14 / v11
w22 = (v22 / v21) - w12
w23 = (v23 / v21) - w13
w24 = (v24 / v21) - w14
p = -w23 / w22
Q = w24 / w22
M = -(w12 * p) - w13
N = w14 - (w12 * Q)
A = (N * N) + (Q * Q) - 1
b = 2 * ((M * N) - (N * C1.Centre.X) + (p * Q) - (Q * C1.Centre.Y) + (Tan1 * C1.Radius))
c = (C1.Centre.X * C1.Centre.X) _
+ (M * M) _
- (2 * M * C1.Centre.X) _
+ (p * p) _
+ (C1.Centre.Y * C1.Centre.Y) _
- (2 * p * C1.Centre.Y) _
- (C1.Radius * C1.Radius)
'Find a root of a quadratic equation (requires the circle centers not to be e.g. colinear)
D = (b * b) - (4 * A * c)
With CTanTanTan(lgTangent)
.Radius = (-b - VBA.Sqr(D)) / (2 * A)
.Centre.X = M + (N * .Radius)
.Centre.Y = p + (Q * .Radius)
End With
Next lgTangent
fApollonius = True
End Function
 
</syntaxhighlight>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
<syntaxhighlight lang="wren">import "./dynamic" for Tuple
 
var Circle = Tuple.create("Circle", ["x", "y", "r"])
 
var solveApollonius = Fn.new { |c1, c2, c3, s1, s2, s3|
var x1 = c1.x
var y1 = c1.y
var r1 = c1.r
 
var x2 = c2.x
var y2 = c2.y
var r2 = c2.r
 
var x3 = c3.x
var y3 = c3.y
var r3 = c3.r
 
var v11 = 2 * x2 - 2 * x1
var v12 = 2 * y2 - 2 * y1
var v13 = x1 * x1 - x2 * x2 + y1 * y1 - y2 * y2 - r1 * r1 + r2 * r2
var v14 = 2 * s2 * r2 - 2 * s1 * r1
 
var v21 = 2 * x3 - 2 * x2
var v22 = 2 * y3 - 2 * y2
var v23 = x2 * x2 - x3 * x3 + y2 * y2 - y3 * y3 - r2 * r2 + r3 * r3
var v24 = 2 * s3 * r3 - 2 * s2 * r2
 
var w12 = v12 / v11
var w13 = v13 / v11
var w14 = v14 / v11
 
var w22 = v22 / v21 - w12
var w23 = v23 / v21 - w13
var w24 = v24 / v21 - w14
 
var p = -w23 / w22
var q = w24 / w22
var m = -w12 * p - w13
var n = w14 - w12 * q
 
var a = n * n + q * q - 1
var b = 2 * m * n - 2 * n * x1 + 2 * p * q - 2 * q * y1 + 2 * s1 * r1
var c = x1 * x1 + m * m - 2 * m * x1 + p * p + y1 * y1 - 2 * p * y1 - r1 * r1
 
var d = b * b - 4 * a * c
var rs = (-b - d.sqrt) / (2 * a)
var xs = m + n * rs
var ys = p + q * rs
return Circle.new(xs, ys, rs)
}
 
var c1 = Circle.new(0, 0, 1)
var c2 = Circle.new(4, 0, 1)
var c3 = Circle.new(2, 4, 2)
System.print("Circle%(solveApollonius.call(c1, c2, c3, 1, 1, 1))")
System.print("Circle%(solveApollonius.call(c1, c2, c3, -1, -1, -1))")</syntaxhighlight>
 
{{out}}
<pre>
Circle(2, 2.1, 3.9)
Circle(2, 0.83333333333333, 1.1666666666667)
</pre>
 
=={{header|XPL0}}==
{{trans|Go}}
<syntaxhighlight lang "XPL0">real Circle(3);
def \Circle\ X, Y, R;
 
proc real AP(C1, C2, C3, S);
real C1, C2, C3; int S;
real X1sq, Y1sq, R1sq,
X2sq, Y2sq, R2sq,
X3sq, Y3sq, R3sq,
V11, V12, V13, V14,
V21, V22, V23, V24,
W12, W13, W14,
W22, W23, W24,
P, Q, M, N, A, B, C, D, RS;
[
X1sq:= C1(X) * C1(X);
Y1sq:= C1(Y) * C1(Y);
R1sq:= C1(R) * C1(R);
X2sq:= C2(X) * C2(X);
Y2sq:= C2(Y) * C2(Y);
R2sq:= C2(R) * C2(R);
X3sq:= C3(X) * C3(X);
Y3sq:= C3(Y) * C3(Y);
R3sq:= C3(R) * C3(R);
V11:= 2. * (C2(X) - C1(X));
V12:= 2. * (C2(Y) - C1(Y));
V13:= X1sq - X2sq + Y1sq - Y2sq - R1sq + R2sq;
V14:= 2. * (C2(R) - C1(R));
V21:= 2. * (C3(X) - C2(X));
V22:= 2. * (C3(Y) - C2(Y));
V23:= X2sq - X3sq + Y2sq - Y3sq - R2sq + R3sq;
V24:= 2. * (C3(R) - C2(R));
if S then [V14:= -V14; V24:= -V24];
W12:= V12 / V11;
W13:= V13 / V11;
W14:= V14 / V11;
W22:= V22/V21 - W12;
W23:= V23/V21 - W13;
W24:= V24/V21 - W14;
P:= -W23 / W22;
Q:= W24 / W22;
M:= -W12*P - W13;
N:= W14 - W12*Q;
A:= N*N + Q*Q - 1.;
B:= M*N - N*C1(X) + P*Q - Q*C1(Y);
if S then B:= B - C1(R)
else B:= B + C1(R);
B:= B * 2.;
C:= X1sq + M*M - 2.*M*C1(X) + P*P + Y1sq - 2.*P*C1(Y) - R1sq;
D:= B*B - 4.*A*C;
RS:= (-B - sqrt(D)) / (2.*A);
Circle(X):= M + N*RS;
Circle(Y):= P + Q*RS;
Circle(R):= RS;
];
 
real C1, C2, C3;
[
C1:= [0., 0., 1.];
C2:= [4., 0., 1.];
C3:= [2., 4., 2.];
AP(C1, C2, C3, true);
RlOut(0, Circle(X)); RlOut(0, Circle(Y)); RlOut(0, Circle(R)); CrLf(0);
AP(C1, C2, C3, false);
RlOut(0, Circle(X)); RlOut(0, Circle(Y)); RlOut(0, Circle(R)); CrLf(0);
]</syntaxhighlight>
{{out}}
<pre>
2.00000 0.83333 1.16667
2.00000 2.10000 3.90000
</pre>
 
=={{header|zkl}}==
{{trans|D}}
<syntaxhighlight lang="zkl">class Circle{
fcn init(xpos,ypos,radius){
var [const] x=xpos.toFloat(), y=ypos.toFloat(),r=radius.toFloat();
}
fcn toString{ "Circle(%f,%f,%f)".fmt(x,y,r) }
fcn apollonius(c2,c3,outside=True){
s1:=s2:=s3:=outside and 1 or -1;
 
v11:=2.0*(c2.x - x);
v12:=2.0*(c2.y - y);
v13:=x.pow(2) - c2.x.pow(2) +
y.pow(2) - c2.y.pow(2) -
r.pow(2) + c2.r.pow(2);
v14:=2.0*(s2*c2.r - s1*r);
 
v21:=2.0*(c3.x - c2.x);
v22:=2.0*(c3.y - c2.y);
v23:=c2.x.pow(2) - c3.x.pow(2) +
c2.y.pow(2) - c3.y.pow(2) -
c2.r.pow(2) + c3.r.pow(2);
v24:=2.0*(s3*c3.r - s2*c2.r);
 
w12,w13,w14:=v12/v11, v13/v11, v14/v11;
w22,w23,w24:=v22/v21 - w12, v23/v21 - w13, v24/v21 - w14;
 
P:=-w23/w22;
Q:= w24/w22;
M:=-w12*P - w13;
N:= w14 - w12*Q;
 
a:=N*N + Q*Q - 1;
b:=2.0*(M*N - N*x + P*Q - Q*y + s1*r);
c:=x*x + M*M - 2.0*M*x + P*P + y*y - 2.0*P*y - r*r;
 
// find a root of a quadratic equation.
// This requires the circle centers not to be e.g. colinear
D:=b*b - 4.0*a*c;
rs:=(-b - D.sqrt())/(2.0*a);
 
Circle(M + N*rs, P + Q*rs, rs);
}
}</syntaxhighlight>
<syntaxhighlight lang="zkl">a,b,c:=Circle(0,0,1), Circle(4,0,1), Circle(2,4,2);
a.apollonius(b,c).println(" Outside");
a.apollonius(b,c,False).println(" Inside");</syntaxhighlight>
{{out}}
<pre>
Circle(2.000000,2.100000,3.900000) Outside
Circle(2.000000,0.833333,1.166667) Inside
</pre>
 
[[Category:Geometry]]