# OpenGL Programming Guide 9th. Help with code

Hey, I’m having difficulty understanding the code of page 250.
The point is what if origin, direction, v0, v1, v2 are all on the same plane.

``````a = -dot(n, w0); a = 0;
b = dot(n, direction); b = 0;

r = a / b; r = 0/0; undefined result.
``````

I also don’t know meaning of “r”. Thanks.

It’s doing this:

but lacks the logic to handle the case where the line is in the plane (i.e. `dot( l, n ) == 0`).

Exactly. Before the divide, it should check whether `b == 0`, and if so do an early `return false` from this `intersect()` function.

I’m not familiar with the code in question, but given Dark_Photon’s comment that it’s a line-plane intersection, it’s safe to assume that `n` is the plane normal, `w0` is some point on the line and `direction` is the line direction. In which case, `r` is the “distance” from `w0` along the line at which the intersection occurs, i.e. the intersection point will be at `w0+r*direction` (if r is negative, then the intersection point is “before” w0, i.e. the direction from w0 to the intersection will be the opposite of `direction`).

`a` is the distance of w0 from the plane, `b` is the amount by which the distance changes when you move by `direction`. So if b=0, the line is parallel to the plane; in that case, if a=0 then the line lies within the plane (i.e. the intersection is the entire line) and if a≠0 then the line never intersections the plane, so:

If you’re dealing with arbitrary geometry, this situation is statistically unlikely. The result of a non-trivial floating-point calculation is rarely exactly equal to zero. So you could just ignore the possibility. However, it could be a realistic possibility if both the plane and the line direction are more likely to be perfectly aligned to coordinate axes than would occur by random chance. If you need to handle this case, it’s better to check whether `abs(b)` is below some small threshold. If it is, the line is almost parallel to the plane and the problem is ill-conditioned, meaning that very small errors in any of the inputs can drastically alter the result; (IOW, whatever result you get may be so far from correct that the actual value isn’t useful).

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.