Skip to content

Instantly share code, notes, and snippets.

@jniemann66
Last active October 4, 2016 01:04
Show Gist options
  • Save jniemann66/10cda980777f23c70ac69ee850c93792 to your computer and use it in GitHub Desktop.
Save jniemann66/10cda980777f23c70ac69ee850c93792 to your computer and use it in GitHub Desktop.
test whether line intersects with circle
///////////////////////////////////////////////////////////////////////////////
// testLineCircleIntersection() : test whether a line intersects with a circle
// x0,y0 : first point on line
// x1,y1 : second point on line
// ox,oy : center of circle
// r: radius of circle
//
// return value:
// < 0 : no intersection,
// = 0 : tangent (hits circle at one point),
// > 0 : intersection (secant; hits circle at two points)
//
// see: http://mathworld.wolfram.com/Circle-LineIntersection.html
//
function testLineCircleIntersection(x0,y0,x1,y1,ox,oy,r) {
// normalize line co-ordinates
x0 -= ox;
y0 -= oy;
x1 -= ox;
y1 -= oy;
// calculate discriminant
let rSquared = r * r;
let dx = x1 - x0;
let dy = y1 - y0;
let bigD = (x0 * y1 - x1 * y0);
let bigDSquared = bigD * bigD;
let lSquared = dx * dx + dy * dy;
let discriminant = rSquared * lSquared - bigDSquared;
return discriminant;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment