line segment colision

Started by
1 comment, last by arthurprs 15 years, 8 months ago
I'm new to this kind of programming, someone can help me with colision between 2 line segments, and know where they intersect (they are determined by 2 points) Thanks.
Advertisement
Here is a function that finds the intersection point between 2 line segments in 2D. The function will return FALSE if line segments do not intersect.

A, B = points on line 1
C, D = points on line 2
Q = intersection point

BOOL IntersectionLineSegments(vec *Q, vec A, vec B, vec C, vec D){    vec U = B - A;    vec V = D - C;    vec W = C - A;    float det  = U.x*V.y-V.x*U.y;    if (det == 0)        return FALSE; // parallel, no intersection    float t = (W.x*U.y-U.x*W.y) / det;    float s = (W.x*V.y-V.x*W.y) / det;    if ((t < 0.0f || t > 1.0f) || (s < 0.0f || s > 1.0f))        return FALSE; // outside line segment    *Q = C + V*t; // intersection point    return TRUE; // line segments intersect}
Thanks, it worked great =D

This topic is closed to new replies.

Advertisement