# Circle-Line intersection

I'm trying to find a way to use Module when plotting a circle and line intersecting at two points on the circle ie: p1 to p2

eg: let's say I use the function circleChord[c,r,line] or given example such as

circleChord[{4, 5}, 5.5, 3.1 x + y == 3]


where it plots the center of the circle having a certain radius r, and the equation of the line segment from p1 to p2.

If you want the intersection points as well, I use the following for a line a*x+b*y+c=0.

IntersectLineCircle[{a_, b_}, c_, Circle[{u_, v_}, r_]] :=
Module[{f = (a^2 + b^2) r^2 - (c + a u + b v)^2},
If[N[f] < 0., {{}, {}},
If[N[a] =!= 0.,
f = Sqrt[f] Abs[a];
1/(a^2 + b^2)
{{(a b^2 u - a^2 (c + b v) - b f)/a, -b (c + a u) + a^2 v + f},
{(a b^2 u - a^2 (c + b v) + b f)/a, -b (c + a u) + a^2 v - f}},
{{u - (Sqrt[-(c + b (v - r)) (c + b (r + v))] Abs[b])/b^2, -(c/b)},
{u + (Sqrt[-(c + b (v - r)) (c + b (r + v))] Abs[b])/b^2, -(c/b)}}
]]]


Example graphic.

With[{a = 3, b = -3, c = 4, u = 2, v = -4, r = 6},
Graphics[{Thick, EdgeForm[{Thick, Black}],
Darker@Green, Disk[{u, v}, r],
Red, Line[{x, (a x + c)/(-b)} /. {{x -> -1.5 r}, {x -> 1.2 r}}],
Blue, PointSize[0.02],
Point[IntersectLineCircle[{a, b}, c, Circle[{u, v}, r]]]
}, Frame -> True]] If there is no intersection of the line and circle, then IntersectLineCircle returns {{},{}}.

• Is there a way to simplify this in terms of using the function for the circle (x - xo)^2 + (y - yo)^2 == r^2, like what it was shown by Bill as well as instead of IntersectLineCircle[{a_, b_}, c_, Circle[{u_, v_}, r_]], use IntersectionLineCircle[{xo_, yo_}, r_, line_]? Apr 2, 2018 at 0:55

Assuming I understood your question correctly (there's really no need to use Module), here's one method which delegates calculating intersections to Solve:

ClearAll@circleChord;

circleChord[{xc_, yc_}, r_, line_] :=
RegionPlot[ImplicitRegion[line, {x, y}],
PlotRange -> {{xc - 2 r, xc + 2 r}, {yc - 2 r, yc + 2 r}},
Epilog -> {Circle[{xc, yc}, r], Red, PointSize@Large,
Point[{x, y}] /. # & /@
Quiet@Solve[line, {x, y} \[Element] Circle[{xc, yc}, r]]}]


circleChord[{4, 5}, 5.5, 3.1 x + y == 3] Most of the above implementation should be relatively self-explanatory, but the Point[{x, y}] /. # & /@ ... part might warrant an explanation: it generates a Point for each solution, and in particular, zero points for zero solutions.

As a bonus, line doesn't really need to be a straight line. This also works:

circleChord[{4, 5}, 5.5, 3.1 x + y^2 == 3] ClearAll[circleChords]
circleChords[{x_, y_}, r_, lines : {{_, _, _} ..}, colors_,  opts : OptionsPattern[]] :=
Module[{col = PadRight[colors, Length @ lines, colors]},
ParametricPlot[r {Cos[t], Sin[t]} + {x, y}, {t, 0, 2 π},
MeshFunctions -> (Function[{u, v}, # u + #2 v - #3 ] & @@@ lines),
Mesh -> ConstantArray[{0}, Length@lines] ,
MeshStyle -> Module[{i = 1}, {Thick, col[[i++]], Line @@ #, PointSize[Large], #} &],
opts]]

circleChords[{4, 5}, 5.5, {{3.1, 1, 3}} ,{Red},Frame -> True, Axes -> False] circleChords[{4, 5}, 5.5, {{3.1, 1, 3}, {3.1, 1, 10} , {3.1, -2, 7}} , {Red, Green, Blue},
Frame -> True, Axes -> False] 