C code example by author
Source code example by: Iebele Abel.
Sphere/ellipse and line intersection code
for Visual Basic by Adrian DeAngelis.
LISP version for AutoCAD (and Intellicad) by Andrew Bennett
intC2.lsp and
intC2_app.lsp.

Points P (x,y) on a line defined by two points P1 (x1,y1,z1) and P2 (x2,y2,z2) is described by
Substituting the equation of the line into the sphere gives a quadratic equation of the form
where:
b = 2[ (x2 - x1) (x1 - x3) + (y2 - y1) (y1 - y3) + (z2 - z1) (z1 - z3) ]
c = x32 + y32 + z32 + x12 + y12 + z12 - 2[x3 x1 + y3 y1 + z3 z1] - r2

The exact behaviour is determined by the expression within the square root
To apply this to two dimensions, that is, the intersection of a line and a circle simply remove the z component from the above mathematics.
When dealing with a line segment it may be more efficient to first determine whether the line actually intersects the sphere or circle. This is achieved by noting that the closest point on the line through P1P2 to the point P3 is along a perpendicular from P3 to the line. In other words if P is the closest point on the line then
(P3 - P) dot (P2 - P1) = 0
Given u, the intersection point can be found, it must also be less than the radius r. If these two tests succeed then the earlier calculation of the actual intersection point can be applied.