|
|
Given a line X = P + s * D and a ray X = Q + t * E with t >= 0. If the line and the ray intersect the distance is naturally 0. If they are parallel we pick the start-point Q on the ray and calculate the minimum distance to the line by projecting Q onto the line and calculating the distance from Q to the projected Q. To calculate the intersection point of the line and the ray we have to solve the following system of equations: P + s * D = Q + t * E or multiplied out p1 + s * d1 = q1 + t * e1 p2 + s * d2 = q2 + t * e2 Solving the second equation for s gives s = (q2 - p2 + t * e2) / d2 Using that in the first equation gives us t as t = (p1 - q1 + d1 / d2 * (q2 - p2)) / (e1 - d1 / d2 * e2) for (e1 - d1 / d2 * e2) != 0 and t >= 0. To avoid divisions by zero we extend with d2 and get t = (-d2 * (q1 - p1) + d1 * (q2 - p2)) / (d2 * e1 - d1 * e2) for (d2 * e1 - d1 * e2) != 0 and t >= 0. To see if the line and ray intersect we need to see if t >= 0 or in other words -d2 * (q1 - p1) + d1 * (q2 - p2) >= 0 <==> d1 * (q2 - p2) >= d2 * (q1 - p1)
|
Copyright © by Martin Ecker |