Created
September 20, 2017 00:13
-
-
Save XProger/51939c7361575d89b7c2e69af8fbc0bb to your computer and use it in GitHub Desktop.
circle vs line segment intersection check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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