# line segment colision

This topic is 3756 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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}

##### Share on other sites
Thanks, it worked great =D

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633313
• Total Posts
3011320
• ### Who's Online (See full list)

There are no registered users currently online

×