6
$\begingroup$

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$?

$\endgroup$
4
  • 2
    $\begingroup$ you could work with ImplicitRegion and RegionCentroid ... $\endgroup$
    – george2079
    Commented Nov 30, 2016 at 13:06
  • $\begingroup$ @george2079 That's a good one (and 1/(2 Pi) RegionMeasure); I suggest you post this as an answer. $\endgroup$
    – corey979
    Commented Nov 30, 2016 at 13:13
  • $\begingroup$ Indeed, this is what I need. $\endgroup$ Commented Nov 30, 2016 at 13:18
  • $\begingroup$ A related question. $\endgroup$ Commented May 24, 2020 at 14:07

3 Answers 3

9
$\begingroup$
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.

$\endgroup$
3
  • $\begingroup$ (+1) I would use == 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$
    – corey979
    Commented Nov 30, 2016 at 15:15
  • $\begingroup$ @corey979 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$
    – george2079
    Commented Nov 30, 2016 at 15:25
  • $\begingroup$ 1/(2 Pi) RegionMeasure@c then. Just for the record, it's minor details. $\endgroup$
    – corey979
    Commented Nov 30, 2016 at 15:29
8
$\begingroup$

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}

$\endgroup$
7
$\begingroup$

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  *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.