# How to optimize the code for solving the Apollonian circle equation?

The distance from a moving point in the plane to (0,0) is lambda times the distance from (a, 0). And lambda is not equal to 1. Find the equation

Clear["Global*"]
expr = EuclideanDistance[{0, 0}, {x,
y}] == \[Lambda] EuclideanDistance[{a, 0}, {x, y}]
eqn = Assuming[Element[{x, y, a, \[Lambda]}, Reals], Simplify[expr]]
ApplySides[#^2 &, eqn]
eq = ApplySides[#^2 &, eqn] // Simplify
"The expression is not quadratic in the variables 1";
CompleteTheSquare[expr_] := CompleteTheSquare[expr, Variables[expr]]
CompleteTheSquare[expr_, Vars_Symbol] :=
CompleteTheSquare[expr, {Vars}]
CompleteTheSquare[expr_, Vars : {__Symbol}] :=
Module[{array, A, B, C, s, vars, sVars},
vars = Intersection[Vars, Variables[expr]];
Check[array = CoefficientArrays[expr, vars], Return[expr],
CoefficientArrays::poly];
Return[expr]];
{C, B, A} = array; A = Symmetrize[A];
s = Simplify[1/2 Inverse[A] . B, Trig -> False];
sVars = Hold /@ (vars + s); A = Map[Hold, A, {2}];
Expand[A . sVars . sVars] + Simplify[C - s . A . s, Trig -> False] //
ReleaseHold]
eq /. Equal -> Subtract
CompleteTheSquare[eq /. Equal -> Subtract, {x, y}]
ApplySides[#/Coefficient[%, y^2] &, % == 0]
Assuming[Element[{x, y, a, \[Lambda]}, Reals], Simplify[%]]
CompleteTheSquare[% /. Equal -> Subtract, {x, y}]
% == 0


get the result:

y^2 - (a^2 \[Lambda]^2)/(-1 + \[Lambda]^2)^2 + (x - (
a \[Lambda]^2)/(-1 + \[Lambda]^2))^2 == 0


How to optimize the code and finally get this result?

y^2 + (x - (a \[Lambda]^2)/(-1 + \[Lambda]^2))^2 == (
a^2 \[Lambda]^2)/(-1 + \[Lambda]^2)^2


Your solution appears to be very extensive.

Here a more direct approach:

 p = {x, y} (* moving point *)
eq = p . p - (p - {a, 0}) . (p - {a, 0}) \[Lambda] ^2 // Expand //FullSimplify
(* x^2 + y^2 - ((a - x)^2 + y^2) \[Lambda]^2 *)


Equation describes a conic section. We try to shift x->m+dx to get a symmetrical form(eliminate linaer term dx)

solm = Solve[Coefficient[ eqm, dx] == 0, m][[1]] (* {m -> (a\[Lambda]^2)/(-1 + \[Lambda]^2)} *)


The remaining equation gl describes a circle with radius=(a \[Lambda])/(-1 + \[Lambda]^2) around centre {(a \[Lambda]^2)/(-1 + \[Lambda]^2),0}

gl = Simplify[(deq /. solm)] /  (-1 + \[Lambda]^2) //Collect[#, {dx, y},Simplify] &
(*-dx^2 - y^2 + (a^2 \[Lambda]^2)/(-1 + \[Lambda]^2)^2*)


gl /. dx -> (m - x) /. solm
`