# How to find the vector that passes through a point and is perpendicular to another vector.

I have 3 points A, B, C. I want to find a vector that passes through B and is perpendicular to the vector AC. How would I go about doing this in Unity?

Hello,

Before I try and answer the question, note that a `Vector` cannot pass through a point. A Vector is merely a direction and magnitude, not a line. Because of this, I assume you are tryign to solve the shortest distance from a line to a point problem.

OK, time for some maths using Unity functions:

Lets say we have the line that passes through the two points A, C and we want to find the vector from that line to B which has the closest distance. By simple logic we can assume that that vector must be perpendicular to the vector AC:

first lets get the equation of the line for A, C:

``````P = A + γ(C - A)
``````

where:

``````P is any point on the line and
γ is any number value
``````

Next lets write the formula for perpendicular vectors as well:

``````(B - P) dot (C - A) = 0
``````

Now that we have two formulas lets solve simultaneously:

``````(1) (B - P) · (C - A) = 0
(2) P = A + γ(C - A)

(2) in (1)
(B - (A + γ(C - A))) · (C - A) = 0
(B - A - γ(C - A)) · (C - A)
``````

Vector dot product:

``````(B.x - A.x - γ(C.x - A.x))(C.x - A.x) + (B.y - A.y - γ(C.y - A.y))(C.y - A.y) + (B.z - A.z - γ(C.z - A.z))(C.z - A.z) = 0
``````

yes I know it’s a lot, just deal with it, so lets make some more variables:

``````D = C - A
O = B - A
``````

Now lets solve for γ

``````(O.x - γD.x)*D.x + (O.y - γD.y)*D.y + (O.z - γD.z)*D.z = 0 | expand
O.x*D.x - γ*D.x^2 + O.y*D.y - γ*D.y^2 + O.z*D.z - γ*D.z^2 = 0 | bring γ to one side
γ*D.x^2 + γ*D.y^2 + γ*D.z^2 = O.x*D.x + O.y*D.y + O.z*D.z | simplify
γ(D · D) = O · D | Solve for γ
γ = (O · D)/(D · D)
``````

Now that we have γ we can get both the closest point on the line, as well as the vector you are looking for:

``````P = A + γD | since we already know γ
``````

and the vector you are looking for is simply:

``````V = P - B
``````

I know it looks complicated, but I just went through the whole proof
the end formula is:

``````V = A + (O · D)/(D · D)*D - B
``````

where D = C - A and O = B - A

I’ll leave the implementation to you
Hope this helps,
Benproductions1

PS: This is not tested, if it doesn’t work (or it’s wrong), please tell me
No, I did not look this up. I simply know my maths

EDIT: Fixed algebra when solving simultaneously thanks to @aldonaletto

@Benproductions1 thanks a lot for this great deduction. Not only it is very well explained, but I couldn’t find anything similar on Internet.

However, after implementing and plotting the results, I believe there is still a little error in the calculation of vector V (second last equation), which causes V having the opposite direction (wrong sign).

You wrote:

V = P - B

but it should be:

``````V = B - P
``````

Consequently, the following (and last) equation becomes:

``````V = B - A - (O · D)/(D · D)*D
``````

Please let me know if this sounds correct to you and, in that case, I agree with @aldonaletto in saying that your original answer deserves to be fixed, as it might be helpful for many others.

Thanks again!