3
$\begingroup$

i'm trying to plot the intersection point between a line and a circle but it's not working. How can I do that using the following command?

Manipulate[Show[ParametricPlot[{r Cos[t] + c, r Sin[t] + d}, {t, 0, 2 Pi}, PlotRange -> {-10, 10}],Plot[Tan[t] x + b, {x, -5 Pi, 5 Pi}]], {t, 0, 2 Pi}, {b, -40, 40}, {r, 0, 8}, {c, -10, 10}, {d, -10, 10}]

I also want to measure the shortest distance between that circle and the line. Am I able to do that using the same variables?

$\endgroup$
1

2 Answers 2

5
$\begingroup$

Here is an example that demonstrates several functions/features that you might want to use:

Manipulate[
  With[{
    regionCircle = Circle[{centerX, centerY}, radius],
    regionLine = InfiniteLine[{0, intercept}, {1, slope}],
    points = 
      NSolveValues[{(x - centerX)^2 + (y - centerY)^2 == radius^2 && y == slope x + intercept}, {x, y}, Reals]},
    Column[{
      Graphics[{
        Blue, regionCircle,
        Purple, regionLine,
        Red, PointSize[.05], Point[points]},
        PlotRange -> {{-20, 20}, {-20, 20}}],
      Row[{"intersection points: ", points}],
      Row[{"distance to center: ", RegionDistance[regionLine, {centerX, centerY}]}],
      Row[{"distance to circumference: ", Max[0, RegionDistance[regionLine, {centerX, centerY}] - radius]}]}]],
  {{centerX, 0, "center x"}, -10, 10},
  {{centerY, 0, "center y"}, -10, 10},
  {{radius, 5, "radius"}, 1, 10},
  {{intercept, 0, "y intercept"}, -10, 10},
  {{slope, 0, "slope"}, -10, 10}]
$\endgroup$
1
  • $\begingroup$ Thank you so much! That'll help me a lot. $\endgroup$
    – Sara
    Dec 4, 2022 at 0:56
3
$\begingroup$

Here my answer using region functionality and userdefined distance function:

Manipulate[kreis = Circle[{xc, yc}, rc];

gerade = {xg, yg} + {Cos[tg], Sin[tg]} u;

mini = NMinimize[# . # &[gerade - {xc, yc}], u];(* minimal distance to circle center*)
dist = If[Sqrt[mini[[1]]] <= rc, 0, Sqrt[mini[[1]]] - rc];
pg = gerade /. mini[[2]];(* nearest linepoint*)
linie = InfiniteLine[{gerade /. u -> 0, gerade /. u -> 1} ];
pts = RegionIntersection[kreis, linie];

Graphics[{kreis, linie, Red, pts, Blue, Point[pg]}, 
PlotLabel -> "RegionDistance " <> ToString[dist],ImageSize->200], {{rc, 1}, 0,8}, {{xc, 0}, -10, 10}, {{yc, 0}, -10, 10}, {{xg, 2}, -10, 10}, {{yg, 1}, -10, 10}, {{tg, 1}, -1,1}]

enter image description here

enter image description here

$\endgroup$

Your Answer

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

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