Problem of Apollonius: Difference between revisions

m
(33 intermediate revisions by 18 users not shown)
Line 3:
 
;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(colored isblack anin [[wp:Problem_of_Apollonius#Algebraic_solutions|algebraicthe solution]]diagram whichbelow isto prettythe straightforwardright).
 
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 (below and right).   The red circle is "internally tangent" to all three black circles, and the green circle is "externally tangent" to all three black circles.
 
 
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, y, r
 
F (x, y, r)
.x = x
.y = y
.r = r
 
F String()
R ‘Circle(x=#., y=#., r=#.)’.format(.x, .y, .r)
 
F solveApollonius(c1, c2, c3, s1, s2, s3)
V (x1, y1, r1) = c1
V (x2, y2, r2) = c2
V (x3, y3, r3) = c3
 
V v11 = 2 * x2 - 2 * x1
V v12 = 2 * y2 - 2 * y1
V v13 = x1 * x1 - x2 * x2 + y1 * y1 - y2 * y2 - r1 * r1 + r2 * r2
V v14 = 2 * s2 * r2 - 2 * s1 * r1
 
V v21 = 2 * x3 - 2 * x2
V v22 = 2 * y3 - 2 * y2
V v23 = x2 * x2 - x3 * x3 + y2 * y2 - y3 * y3 - r2 * r2 + r3 * r3
V v24 = 2 * s3 * r3 - 2 * s2 * r2
 
V w12 = v12 / v11
V w13 = v13 / v11
V w14 = v14 / v11
 
V w22 = v22 / v21 - w12
V w23 = v23 / v21 - w13
V w24 = v24 / v21 - w14
 
V P = -w23 / w22
V Q = w24 / w22
V M = -w12 * P - w13
V n = w14 - w12 * Q
 
V a = n * n + Q * Q - 1
V b = 2 * M * n - 2 * n * x1 + 2 * P * Q - 2 * Q * y1 + 2 * s1 * r1
V c = x1 * x1 + M * M - 2 * M * x1 + P * P + y1 * y1 - 2 * P * y1 - r1 * r1
 
V D = b * b - 4 * a * c
V rs = (-b - sqrt(D)) / (2 * a)
 
V xs = M + n * rs
V ys = P + Q * rs
 
R Circle(xs, ys, rs)
 
V (c1, c2, c3) = (Circle(0, 0, 1), Circle(4, 0, 1), Circle(2, 4, 2))
print(solveApollonius(c1, c2, c3, 1, 1, 1))
print(solveApollonius(c1, c2, c3, -1, -1, -1))</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:
<langsyntaxhighlight Adalang="ada">package Apollonius is
type Point is record
X, Y : Long_Float := 0.0;
Line 26 ⟶ 95:
T1, T2, T3 : Tangentiality := External)
return Circle;
end Apollonius;</langsyntaxhighlight>
apollonius.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Generic_Elementary_Functions;
 
package body Apollonius is
Line 113 ⟶ 182:
end;
end Solve_CCC;
end Apollonius;</langsyntaxhighlight>
 
example test_apollonius.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Apollonius;
 
Line 140 ⟶ 209:
Long_Float_IO.Put (R2.Radius, Aft => 3, Exp => 0);
Ada.Text_IO.New_Line;
end Test_Apollonius;</langsyntaxhighlight>
 
output:
Line 147 ⟶ 216:
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
2.0 0.8333333333333333 1.166666666666667</pre>
 
=={{header|AutoHotkey}}==
{{libheader|GDIP}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">#NoEnv
#SingleInstance, Force
SetBatchLines, -1
Line 254 ⟶ 476:
Gdip_DrawCircle(G, pPen, x, y, r) {
Return Gdip_DrawEllipse(G, pPen, x-r, y-r, r*2, r*2)
}</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="bbcbasic"> DIM Circle{x, y, r}
DIM Circles{(2)} = Circle{}
Circles{(0)}.x = 0 : Circles{(0)}.y = 0 : Circles{(0)}.r = 1
Line 295 ⟶ 576:
c.x = c.r * w(2) - w(1)
c.y = c.r * u(2) - u(1)
ENDPROC</langsyntaxhighlight>
'''Output:'''
<pre>
Line 304 ⟶ 585:
=={{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.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <tgmath.h>
 
Line 413 ⟶ 694:
puts("set 2"); apollonius(b);
puts("set 3"); apollonius(c);
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
This code finds all 8 possible circles touching the three given circles.
<langsyntaxhighlight lang="csharp">
using System;
 
Line 574 ⟶ 855:
}
}
</syntaxhighlight>
</lang>
 
=={{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}}
<langsyntaxhighlight lang="coffeescript">
class Circle
constructor: (@x, @y, @r) ->
Line 631 ⟶ 1,007:
console.log apollonius(c1, c2, c3, -1, -1, -1)
 
</syntaxhighlight>
</lang>
output
<syntaxhighlight lang="text">
> coffee foo.coffee
{ x: 0, y: 0, r: 1 }
Line 640 ⟶ 1,016:
{ x: 2, y: 2.1, r: 3.9 }
{ x: 2, y: 0.8333333333333333, r: 1.1666666666666667 }
</syntaxhighlight>
</lang>
 
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight lang="d">import std.stdio, std.math;
 
immutable struct Circle { double x, y, r; }
Line 723 ⟶ 1,099:
alias Ti = Tangent.internally;
solveApollonius(c1, c2, c3, Ti, Ti, Ti).writeln;
}</langsyntaxhighlight>
{{out}}
<pre>immutable(Circle)(2, 2.1, 3.9)
immutable(Circle)(2, 0.833333, 1.16667)
</pre>
 
=={{header|Dyalect}}==
{{trans|Swift}}
<syntaxhighlight lang="dyalect">type Circle(Array center, Float radius) with Lookup
func Circle.ToString() =>
"Circle[x=\(this.center[0]),y=\(this.center[1]),r=\(this.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>
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}}
<langsyntaxhighlight lang="elixir">defmodule Circle do
def apollonius(c1, c2, c3, s1, s2, s3) do
{x1, y1, r1} = c1
Line 763 ⟶ 1,283:
IO.inspect Circle.apollonius(c1, c2, c3, 1, 1, 1)
IO.inspect Circle.apollonius(c1, c2, c3, -1, -1, -1)</langsyntaxhighlight>
 
{{out}}
Line 773 ⟶ 1,293:
=={{header|F_Sharp|F#}}==
{{trans|OCaml}}
<langsyntaxhighlight lang="fsharp">type point = { x:float; y:float }
type circle = { center: point; radius: float; }
 
Line 839 ⟶ 1,359:
let r2 = solve_apollonius c1 c2 c3 (-1.) (-1.) (-1.)
print_circle r2
0</langsyntaxhighlight>
{{out}}
<pre>Circle(x=2.00, y=2.10, r=3.90)
Line 846 ⟶ 1,366:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">program Apollonius
implicit none
 
Line 917 ⟶ 1,437:
 
end function
end program</langsyntaxhighlight>
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))
x2 = Val(Mid(c2, 3, 1)): y2 = Val(Mid(c2, 11, 1)): r2 = Val(Mid(c2, 19, 1))
x3 = Val(Mid(c3, 3, 1)): y3 = Val(Mid(c3, 11, 1)): r3 = Val(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 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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 987 ⟶ 1,679:
rs := (-b - math.Sqrt(d)) / (2 * a)
return circle{m + n*rs, p + q*rs, rs}
}</langsyntaxhighlight>
Output:
<pre>
Line 996 ⟶ 1,688:
=={{header|Haskell}}==
{{trans|D}}
<langsyntaxhighlight lang="haskell">data Circle = Circle { x, y, r :: Double } deriving (Show, Eq)
data Tangent = Externally | Internally deriving Eq
 
Line 1,070 ⟶ 1,762:
 
let ti = Internally
print $ solveApollonius c1 c2 c3 ti ti ti</langsyntaxhighlight>
{{out}}
<pre>Circle {x = 2.0, y = 2.1, r = 3.9}
Line 1,078 ⟶ 1,770:
This is a translation of the Java version. [[File:Apollonius-unicon.png|thumb|Solution for Apollonius]]
 
<langsyntaxhighlight Iconlang="icon">link graphics
 
record circle(x,y,r)
Line 1,145 ⟶ 1,837:
ys := P + Q * rs
return circle(xs,ys,rs)
end</langsyntaxhighlight>
 
Output:<pre>Circle(x,y,r) := (0, 0, 1)
Line 1,155 ⟶ 1,847:
=={{header|J}}==
'''Solution'''
<langsyntaxhighlight lang="j">require 'math/misc/amoeba'
 
NB.*apollonius v solves Apollonius problems
Line 1,173 ⟶ 1,865:
avg=. +/ % #
(, avg@dist) soln
)</langsyntaxhighlight>
 
'''Usage'''
<langsyntaxhighlight lang="j"> ]rctst=: 0 0 1,4 0 1,:2 4 2 NB. Task circles
0 0 1
4 0 1
Line 1,183 ⟶ 1,875:
2 0.83333333 1.1666667
2 2.1 3.9
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
<langsyntaxhighlight Javalang="java">public class Circle
{
public double[] center;
Line 1,278 ⟶ 1,970:
System.out.println(solveApollonius(c1,c2,c3,-1,-1,-1));
}
}</langsyntaxhighlight>
 
 
=={{header|jq}}==
{{trans|Go}}
{{works with|jq|1.4}}
<langsyntaxhighlight lang="jq">def circle:
{"x": .[0], "y": .[1], "r": .[2]};
 
Line 1,334 ⟶ 2,025:
| [$m + ($n*$rs), $p + ($q*$rs), $rs]
| circle
;</langsyntaxhighlight>
'''The task''':
<langsyntaxhighlight lang="jq">def task:
([0, 0, 1] | circle) as $c1
| ([4, 0, 1] | circle) as $c2
Line 1,342 ⟶ 2,033:
| ( ap($c1; $c2; $c3; true), # interior
ap($c1; $c2; $c3; false) ) # exterior
;</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight lang="sh">$ jq -n -c -f apollonius.jq
{"x":2,"y":0.8333333333333333,"r":1.1666666666666667}
{"x":2,"y":2.1,"r":3.9}</langsyntaxhighlight>
 
=={{header|Julia}}==
Line 1,354 ⟶ 2,045:
 
'''Module''':
<syntaxhighlight lang ="julia">using Printf
 
module ApolloniusProblems
 
using Polynomials, LinearAlgebra, Printf
export Circle
 
Line 1,395 ⟶ 2,086:
d = (x[1] ^ 2 + y[1] ^ 2 - r[1] ^ 2) .- (x[2:3] .^ 2 .+ y[2:3] .^ 2 .- r[2:3] .^ 2)
end
u = PolyPolynomial([-det([b d]), det([b c])] ./ det([a b]))
v = PolyPolynomial([det([a d]), -det([a c])] ./ det([a b]))
w = PolyPolynomial([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))
Line 1,403 ⟶ 2,094:
length(r) == 1 || error("There is no solution.")
r = r[1]
return Circle(polyvalevalpoly(ur, ru), polyvalevalpoly(vr, rv), r)
end
 
end # module ApolloniusProblem</langsyntaxhighlight>
 
'''Main''':
<langsyntaxhighlight 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)]
Line 1,416 ⟶ 2,107:
println("The internal circle is:\n\t", ApolloniusProblems.solve(test))
println("The external circle is:\n\t", ApolloniusProblems.solve(test, 1:3))
end</langsyntaxhighlight>
 
{{out}}
Line 1,430 ⟶ 2,121:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.3
 
data class Circle(val x: Double, val y: Double, val r: Double)
Line 1,482 ⟶ 2,173:
println(solveApollonius(c1, c2, c3, 1, 1, 1))
println(solveApollonius(c1, c2, c3,-1,-1,-1))
}</langsyntaxhighlight>
 
{{out}}
Line 1,492 ⟶ 2,183:
=={{header|Lasso}}==
{{trans|Java}}
<langsyntaxhighlight Lassolang="lasso">define solveApollonius(c1, c2, c3, s1, s2, s3) => {
local(
x1 = decimal(#c1->get(1)),
Line 1,551 ⟶ 2,242:
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>
</lang>
{{out}}
<pre>staticarray(2.000000, 2.100000, 3.900000)
Line 1,558 ⟶ 2,249:
=={{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">
<lang lb>
circle1$ =" 0.000, 0.000, 1.000"
circle2$ =" 4.000, 0.000, 1.000"
Line 1,613 ⟶ 2,304:
ApolloniusSolver$ =using( "###.###", XPos) +"," +using( "###.###", YPos) +using( "###.###", Radius)
end function
</syntaxhighlight>
</lang>
x_pos y_pos radius
Line 1,622 ⟶ 2,313:
2.000, 2.100, 3.900
2.000, 0.833, 1.167
 
=={{header|MathematicaLua}}==
 
<lang Mathematica>Apolonius[a1_,b1_,c1_,a2_,b2_,c2_,a3_,b3_,c3_,S1_,S2_ ,S3_ ]:=
<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;
Line 1,652 ⟶ 2,405:
rs = (-b -Sqrt[d])/(2*a);
xs = m + n*rs; ys = p + q*rs;
Map[N,{xs, ys, rs} ]]</langsyntaxhighlight>
 
<pre>Apolonius[0,0,1,2,4,2,4,0,1,1,1,1]
Line 1,662 ⟶ 2,415:
=={{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 1,703 ⟶ 2,456:
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 1,719 ⟶ 2,472:
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang="nim">import math
 
type Circle = tuple[x, y, r: float]
Line 1,766 ⟶ 2,519:
 
echo solveApollonius(c1, c2, c3, 1.0, 1.0, 1.0)
echo solveApollonius(c1, c2, c3, -1.0, -1.0, -1.0)</langsyntaxhighlight>
Output:
<pre>(x: 2.0000000000000000e+000, y: 2.1000000000000001e+001, r: 3.8999999999999999e+009)
(x: 2.0000000000000000e+000, y: 80.3333333333333326e-018333333333333333, r: 1.1666666666666667e+00166666666666667)</pre>
 
=={{header|OCaml}}==
{{trans|C}}
 
<langsyntaxhighlight lang="ocaml">type point = { x:float; y:float }
type circle = {
center: point;
Line 1,849 ⟶ 2,602:
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|Perl 6Raku}}
<langsyntaxhighlight lang="perl">use utf8;
use Math::Cartesian::Product;
 
Line 1,926 ⟶ 2,679:
for (cartesian {@_} ([-1,1])x3) {
print Circle->show( solve_Apollonius $c1, $c2, $c3, @$_);
}</langsyntaxhighlight>
{{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|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.
 
<lang perl6>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;
}</lang>
{{out}}
<pre>x = 2.000 y = 0.833 r = 1.167
Line 2,007 ⟶ 2,691:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function Apollonius(sequence calc, circles)
<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>
 
integer {s1,s2,s3} = calc
<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>
 
atom {x1,y1,r1} = circles[1],
<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>
{x2,y2,r2} = circles[2],
<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>
{x3,y3,r3} = circles[3],
<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>
 
v11 = 2*x2 - 2*x1,
<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>
v12 = 2*y2 - 2*y1,
<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>
v13 = x1*x1 - x2*x2 + y1*y1 - y2*y2 - r1*r1 + r2*r2,
<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>
v14 = 2*s2*r2 - 2*s1*r1,
<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>
v21 = 2*x3 - 2*x2,
<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>
v22 = 2*y3 - 2*y2,
<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>
v23 = x2*x2 - x3*x3 + y2*y2 - y3*y3 - r2*r2 + r3*r3,
<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>
v24 = 2*s3*r3 - 2*s2*r2,
<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>
w12 = v12 / v11,
<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>
w13 = v13 / v11,
<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>
w14 = v14 / v11,
<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>
w22 = v22 / v21 - w12,
<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>
w23 = v23 / v21 - w13,
<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>
w24 = v24 / v21 - w14,
<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>
P = -w23 / w22,
<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>
Q = w24 / w22,
<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>
M = -w12*P - w13,
<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>
N = w14 - w12*Q,
<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>
a = N*N + Q*Q - 1,
<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>
b = 2*M*N - 2*N*x1 + 2*P*Q - 2*Q*y1 + 2*s1*r1,
<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>
c = x1*x1 + M*M - 2*M*x1 + P*P + y1*y1 - 2*P*y1 - r1*r1,
<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>
d = b*b - 4*a*c,
<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>
rs = (-b-sqrt(d)) / (2*a),
<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>
 
xs = M + N*rs,
<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>
ys = P + Q*rs
<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>
 
return {xs,ys,rs}
<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>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
constant circles = {{0,0,1},
<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>
{4,0,1},
<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>
{2,4,2}}
<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>
 
-- +1: externally tangental, -1: internally tangental
<span style="color: #000080;font-style:italic;">-- +1: externally tangental, -1: internally tangental</span>
constant calcs = {{+1,+1,+1},
<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>
{-1,-1,-1},
<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>
{+1,-1,-1},
<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>
{-1,+1,-1},
<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>
{-1,-1,+1},
<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>
{+1,+1,-1},
<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>
{-1,+1,+1},
<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>
{+1,-1,+1}}
<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>
for i=1 to 8 do
<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>
atom {xs,ys,rs} = Apollonius(calcs[i],circles)
<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>
string th = {"st (external)","nd (internal)","rd","th"}[min(i,4)]
<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>
printf(1,"%d%s solution: x=%+f, y=%+f, r=%f\n",{i,th,xs,ys,rs})
<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>
end for</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,083 ⟶ 2,769:
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">Apollonius: procedure options (main); /* 29 October 2013 */
 
define structure
Line 2,151 ⟶ 2,837:
return (res);
end Solve_Apollonius;
end Apollonius;</langsyntaxhighlight>
Results:
<pre>
Line 2,159 ⟶ 2,845:
=={{header|PowerShell}}==
{{trans|C#}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Measure-Apollonius
{
Line 2,229 ⟶ 2,915:
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<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>
</lang>
{{Out}}
<pre>
Line 2,252 ⟶ 2,938:
=={{header|PureBasic}}==
{{trans|Java}}
<langsyntaxhighlight PureBasiclang="purebasic">Structure Circle
XPos.f
YPos.f
Line 2,325 ⟶ 3,011:
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 2,332 ⟶ 3,018:
=={{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 2,388 ⟶ 3,074:
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)
Line 2,395 ⟶ 3,081:
=={{header|Racket}}==
{{trans|Java}}
<syntaxhighlight lang="racket">
<lang Racket>
#lang slideshow
 
Line 2,480 ⟶ 3,166:
"green" (apollonius c1 c2 c3 1.0 1.0 1.0)
"red" (apollonius c1 c2 c3 -1.0 -1.0 -1.0))
</syntaxhighlight>
</lang>
 
=={{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.
<langsyntaxhighlight 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. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 2,510 ⟶ 3,266:
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
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</langsyntaxhighlight>
Programming note: &nbsp; in REXX, dividing by unity normalizes the number.
Line 2,526 ⟶ 3,281:
{{trans|Java}}
 
<langsyntaxhighlight lang="ruby">class Circle
def initialize(x, y, r)
@x, @y, @r = [x, y, r].map(&:to_f)
Line 2,582 ⟶ 3,337:
 
puts Circle.apollonius(c1, c2, c3)
puts Circle.apollonius(c1, c2, c3, -1, -1, -1)</langsyntaxhighlight>
 
{{out}}
Line 2,594 ⟶ 3,349:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">object ApolloniusSolver extends App {
case class Circle(x: Double, y: Double, r: Double)
object Tangent extends Enumeration {
Line 2,674 ⟶ 3,429:
}
}
}</langsyntaxhighlight>
Output:
<pre>
Line 2,694 ⟶ 3,449:
 
=={{header|Sidef}}==
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="ruby">class Circle(x,y,r) {
method to_s { "Circle(#{x}, #{y}, #{r})" }
}
Line 2,742 ⟶ 3,497:
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>);</langsyntaxhighlight>
{{out}}
<pre>
Line 2,751 ⟶ 3,506:
=={{header|Swift}}==
{{trans|Java}}
<langsyntaxhighlight Swiftlang="swift">import Foundation
 
struct Circle {
Line 2,822 ⟶ 3,577:
 
println(solveApollonius(c1,c2,c3,1,1,1).toString())
println(solveApollonius(c1,c2,c3,-1,-1,-1).toString())</langsyntaxhighlight>
{{out}}
<pre>Circle[x=2.0,y=2.1,r=3.9]
Line 2,830 ⟶ 3,585:
{{trans|Java}}
{{works with|Tcl|8.6}} or {{libheader|TclOO}}
<langsyntaxhighlight lang="tcl">package require TclOO; # Just so we can make a circle class
 
oo::class create circle {
Line 2,885 ⟶ 3,640:
 
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 2,893 ⟶ 3,648:
set sB [solveApollonius $c1 $c2 $c3 -1 -1 -1]
puts [$sA format]
puts [$sB format]</langsyntaxhighlight>
Output:
<pre>
Line 2,905 ⟶ 3,660:
=={{header|VBA}}==
 
<langsyntaxhighlight VBAlang="vba/VBasicvbasic 6.0">
Option Explicit
Option Base 0
Line 3,103 ⟶ 3,858:
End Function
 
</syntaxhighlight>
</lang>
 
=={{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}}
<langsyntaxhighlight lang="zkl">class Circle{
fcn init(xpos,ypos,radius){
var [const] x=xpos.toFloat(), y=ypos.toFloat(),r=radius.toFloat();
Line 3,148 ⟶ 4,042:
Circle(M + N*rs, P + Q*rs, rs);
}
}</langsyntaxhighlight>
<langsyntaxhighlight 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");</langsyntaxhighlight>
{{out}}
<pre>
1,983

edits