I have two rays on a 2D plane that extend to infinity, but both have a starting point. They are both described by a starting point and a vector in the direction of the ray extending to infinity. I want to find out if the two rays intersect, but I don't need to know where they intersect (it's part of a collision detection algorithm).
Everything I have looked at so far describes finding the intersection point of two lines or line segments. Is there a fast algorithm to solve this?
A ray can be represented by the set of points
A + Vt
, whereA
is the starting point,V
is a vector indicating the direction of the ray, andt >= 0
is the parameter. Thus, to determine if two rays intersect, do this:Given: two rays a, b with starting points (origin vectors) as, bs, and direction vectors ad, bd.
The two lines intersect if there is an intersection point p:
If this equation system has a solution for u>=0 and v>=0 (the positive direction is what makes them rays), the rays intersect.
For the x/y coordinates of the 2d vectors, this means:
Further steps:
Solving against v:
Inserting and solving against u:
Calculate u, then calculate v, if both are positive the rays intersect, else not.
I want only to check if the two rays intersect. I will go about it by calculating the direction of rotation of two "triangles" created from the two rays. They aren't really triangles, but from a mathematical standpoint, if I only wanted to calculate the rotation of the triangle, I only need two vectors with a common starting point, and the rest doesn't matter.
The first triangle will be formed by two vectors and a starting point. The starting point will be the first ray's starting point. The first vector will be the first ray's direction vector. The second vector will be the vector form the first ray's starting point to the second ray's starting point. From here we take the cross product of the two vectors and note the sign.
We do this again for the second triangle. Again, the starting point is the second ray's starting point. The first vector is the second ray's direction and the second vector is from the second ray's starting point to the first ray's starting point. We take the cross product again of the vectors and note the sign.
Now we simply take the two signs and check if they are the same. If they are the same, we have no intersection. If they are different we have an intersection. That's it!
Here's some psudo code:
It works out to be five multiplications, six subtractions, and one comparison.
I am sorry to disagree with the answer of Peter Walser. Solving the equations gives on my desk:
Factoring out the common terms, this comes to:
Five subtractions, six multiplications and two divisions.
If you only need to know if the rays intersect, the signs of u and v are enough, and these two divisons can be replaced by num*denom<0 or (sign(num) != sign(denom)), depending on what is more efficient on your target machine.
Please note that the rare case of det==0 means that the rays do not intersect (one additional comparison).
If the lines are of infinite length, then they will always intersect, unless they are parallel. To check if they are parallel, find the slope of each line and compare them. The slope will just be (y2-y1)/(x2-x1).
Lines are represented by a point p and a vector v:
Rays are (the positive) half of that line:
To determine if two lines intersect, set them equal and solve:
Solve for a1 and a2 - if they are both non-negative, they intersect. If one is negative, they don't intersect.