I have an equation of a circle:
x^2+y^2+2x-15==0
Does Mathematica have a command that can directly get the center coordinates $(x,y)$ and radius $r$?
c = ImplicitRegion[x^2 + y^2 + 2 x - 15 < 0, {x, y}];
center = RegionCentroid[c]
radius = Sqrt[Area[c]/Pi]
{-1, 0}
4
Note this does not verify if the input is actually a circle.
==
instead of <
so that no matter what's on the left/right side of the equation it'll work (assuming it's a circle, of course).
$\endgroup$
RegionCentroid
is much slower with the equality for some reason, it does work though. You need to use ArcLength
instead of Area
to get the radius.
$\endgroup$
Commented
Nov 30, 2016 at 15:25
1/(2 Pi) RegionMeasure@c
then. Just for the record, it's minor details.
$\endgroup$
No, but you can easily implement your own function:
f[formula_] := {{a, b}, r} /.
Solve[{-2 a == Coefficient[formula, x], -2 b == Coefficient[formula, y],
a^2 + b^2 - r^2 == Last@MonomialList[formula], r > 0}, {a, b, r}][[1]]
so that
eq = x^2 + y^2 + 2 x - 15;
f[eq]
{{-1, 0}, 4}
Method 1:
circleForm = (x - a)^2 + (y - b)^2 - r^2;
sol = SolveAlways[x^2 + y^2 + 2 x - 15 == circleForm, {x, y}];
Pick[sol, NonNegative[r /. sol]]
(* {{a -> -1, b -> 0, r -> 4}} *)
As a function:
centerRadius[equation_, {x_, y_}] := Module[{res, a, b, r},
res = SolveAlways[
(equation /. Equal -> Subtract) == (x - a)^2 + (y - b)^2 - r^2,
{x, y}];
(* add check/message if SolveAlways fails, if desired *)
({{a, b}, r} /. Pick[res, NonNegative[r /. res]]) /; res =!= {}
];
centerRadius[x^2 + y^2 + 2 x - 15 == 0, {x, y}]
(* {{{-1, 0}, 4}} *)
Method 2: The gradient vanishes at the center; value of form at center is ±1 times the square of the radius, depending on the sign of the coefficients of the quadratic terms.
eqn = x^2 + y^2 + 2 x - 15 == 0;
center = First@Solve[D[eqn, {{x, y}}]]
radius = Sqrt[-(eqn /. Equal -> Subtract /. center)] (* mult. by -1 * sign(coeff(x^2)) *)
(*
{x -> -1, y -> 0}
4
*)
Circle check (for checking function arguments, if desired):
circleQ[form_, {x_, y_}] := With[{ca = CoefficientArrays[form, {x, y}]},
TrueQ[Length[ca] == 3] &&
MatchQ[Normal@ca[[3]], {{a_, 0}, {0, a_}}]
];
circleQ[x^2 + y^2 + 2 x - 15, {x, y}]
(* True *)
1/(2 Pi) RegionMeasure
); I suggest you post this as an answer. $\endgroup$