Math in Unity : Intersections (Part V)

In previous articles we have seen what happens when we have two line which intersect one with each other.
But if we have two parallel lines?

Let’s try this :

Obviously, if we have this scenario, we have an error, because two parallel lines don’t have an intersection right?
So we can do a check, inside our IntersectionPoint () to see if the line we pass as a parameter is parallel or not.

If you remember, the formula to check if two lines are parallel if :
the Dot Product of Perp of one Vector with other Vector.
If the result is zero, the two lines are parallel.

Inside IntersectionPoint(), we make a check :

Now we have a tricky part.
As we can see, we need to return a float.
The problem here is this : because the lines are infinite, every value of “t” is a legitimate value.
Sometimes , for example, we return a value like -1, but in this case -1 is a legitimate value, so how can we fix this?

We return a


Here the reference

Wikipedia reference:

I know that this seems complicated, but after we see how to use probably is little more “easy” to understand.

In fact, in DrawIntersection script we need to create a strange compare, because the float.NaN in C# works a bit strange.

This because if we compare something == NaN, the result is false.
so we need to create a strange compare, like this :

In this case Visual Studio gives us a strange green line under our “compare” because, well, we compare a value with itself so obviously is the same value…
But the fact is that :
if intersectT is a NaN (Not a Number) and intersectT is a NaN (Not a Number) it means aren’t the same, in a really strange ways but the logic in fact is this.
Because if they are legitimate value, return true.

I know this is a bit of a “strange” concept to understand, and I hope I have explained myself well enough to understand its meaning.

If we test our code now, we don’t have any error and we don’t have any sphere Instantied, becuse the lines are parallel.

In the next article we see some other examples on the lines.