I tried some of the formulas answered here, but none of them works within the constraints of my problem.
Here is it:
I got two points – P1 and P2 and a line – [Pa, Pb]. Inbetween these points, I detect a intersection with the line, giving the point P3.
The problem is – I have to find the projected position of P2 into line [Pa, Pb] and this projection needs to be parallel to a 90º world degree angle.
I've got a gif for you guys with two examples:
I've already found P3.
So, with P1, P2, P3 (if there is an interesection) and the direction of the line (Pa – Pb) normalized, I have to find point X
Best Answer
Suppose $P_2$ has coordinates $x_2$ and $y_2.$ Then you are asking for a point on the line $P_aP_b$ that has either the same $x$ coordinate or the same $y$ coordinate.
Take the equation of the line and substitute $x=x_2$ so $y$ is the only unknown, and solve for $y$. If there is a solution, that gives you the point projected vertically. Next, in the original equation, substitute $y=y_2$ and solve for $x.$ If there is a solution, it’s your horizontal projection. Finally, choose the closer point.
If you find the slope of the line (using the equation) you can just compute one point. If the slope is between $-\frac12$ and $\frac12$ then the vertical projection will be the closest one; otherwise the horizontal projection is the one you want.