Time to code!
“Unfortunately” the theory is important, so I had to write two articles about it.
Now, we have seen that to calculate both t and s we need the perpendicular Vector.
First, let’s create a new C # script.
What we need are three variables and a constructor.
Now we can create a new method for calculating the perpendicular.
Nothing complex, as we know the perpendicular is given by (-y, x).
So, our Perp() method will have one parameter, a Vector, and will return its perpendicular to us.
Other than that, we definitely need the Dot product.
The method is the same as we have already seen.
To see the result we will also need our DrawLine() method again.
Since we will need a second script, we also create a Draw() method which will contain the DrawLine() method
Once this is done, we can turn to the second script.
Which is what we will use to draw our lines.
We have two LineScript type variables to create two different lines.
The coordinates are absolutely random, the important thing is that they cross each other.
Now that we have the two lines, we need to write the formula
First of all we need Vector c.
Which if you remember, is the difference between point A and point B.
Which are nothing more than the starting points of the two lines.
Let’s explain what happens:
using the LineScript line as a parameter, we can compute Vector C by subtracting line.A from A.
This is because A represents a line, while line.A represents the line we are passing as a parameter.
And now we calculate t.
As we can see from the formula,
t = the Dot product of the perpendicular u (in our case the Vector v) and c divided by the Dot product of the perpendicular u and v.
Now we can return t
In the next article we finish the process!