Skip to content

Instantly share code, notes, and snippets.

@XProger
Created September 20, 2017 00:13
Show Gist options
  • Save XProger/51939c7361575d89b7c2e69af8fbc0bb to your computer and use it in GitHub Desktop.
Save XProger/51939c7361575d89b7c2e69af8fbc0bb to your computer and use it in GitHub Desktop.
circle vs line segment intersection check
bool Circle::intersect(const Segment2D &segment, vec2 &n, float &t) const {
vec2 ap = center - segment.a;
vec2 ab = segment.b - segment.a;
float dab = ab.dot(ab);
if (dab == 0.0f) return false;
t = clamp(ap.dot(ab) / dab, 0.0f, 1.0f);
n = center - (segment.a + ab * t);
t = n.length2();
if (t > radius * radius)
return false;
n = n.normal();
t = radius - sqrtf(t);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment