TITLE: Check if line intersects with circles perimeter
QUESTION [19 upvotes]: Say I have this circle here. 
What would be an algorithm to check if lines like the green or blue lines intersect the edge of the circle, but not the red line.
So lets say
if(amountOfPointsHit(line, circle) > 1) then return true
else return false


I only know the start and finish points of the lines, and not where the line intersects the circle. So the pseudo code would more be like 
if(pointBetweenXYHit(x1, y1, x2, y2, circle) > 1) then return true
else return false

Here is the code I came up with based on the answers...
/**
*@param l1  Line point 1, containing latitude and longitude
*@param l2  Line point 2, containing latitude and longitude
*@param c   Center of circle, containing latitude and longitud
*@param r   Radius of the circle
**/
Maps.ui.inCircle = function(l1, l2, c, r){
    var a = l1.lat() - l2.lat()
    var b = l1.lng() = l2.lng()
    var x = Math.sqrt(a*a + b*b)
    return (Math.abs((c.lat() - l1.lat()) * (l2.lng() - l1.lng()) - 
           (c.lng() -  l1.lng()) * (l2.lat() - l1.lat())) / x <= r);
}

pseudo code for that ^^ is 
method inCircle (line1point, line2point, center, radius)
    let l1 = line1point
    let l2 = line2point
    let c = center
    let r = radius
    x is length between points line1point and line2point
    return true if ((c.x - l1.x) * (l2.y - l1.y) - (c.y - l1.y) * (l2.x - l1.x)) / x) 
    is less then or eqaul to r

REPLY [3 votes]: $\newcommand{\norm}[1]{\lVert #1 \rVert}
\newcommand\inner[2]{\langle #1, #2 \rangle}$
Frequently in computer graphics, if you work through the math in terms of vectors instead of coordinates, it makes the math simpler and works for any dimension. Say $v_1$ and $v_2$ are the endpoints of the line segment. Let $v = v_2 - v_1$ be a vector and WLOG let $C$ be a circle (or sphere in higher dimensions) centered at the origin with radius $r$. Then points on the line segment are given by $x = v_1 + t v$ for $0 \le t \le 1$.
We know points on the sphere are given by $\norm{x} = r$. Now we can simply solve $\norm{v_1 + tv}^2 = r^2$ and if there are real solutions we can check the values of $t$ to see if the intersections are within the line segment.
This gives us the quadratic equation in $t$
$$\norm{v}^2 t^2 + 2 \inner{v_1}{v} t + (\norm{v_1}^2 - r^2) = 0$$
We get real solutions for $t$ if discriminant $\Delta = 4 \inner{v_1}{v}^2 - 4\norm{v}^2 (\norm{v_1}^2 - r^2) \ge 0$, and the line is tangent if $\Delta = 0$. We can check if the intersections happen within the line segment by checking if
$$
t = \frac{-2 \inner{v_1}{v} \pm \sqrt \Delta} {2 \norm{v}^2} 
= \frac{- \inner{v_1}{v} \pm \sqrt {\Delta/4}}{\norm{v}^2}
$$
is between $0$ and $1$. These can be simplified in terms of coordinates for special cases of 2D and 3D and should be simplified in code for computational efficiency. For example, to just check if there are intersections for an infinite line, we only need to check if $r^2 ((x_2 - x_1)^2 + (y_2 - y_1)^2) \ge x_1 y_2 - x_2 y_1$. No division or squareroots in this computation!