1

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:

this is hard

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

  • Since $P_3$ is already on the line $P_aP_b,$ any projection onto the line would map it to itself. You clearly don’t want that. In fact it seems in your examples you are projecting $P_2$ (not $P_3$) onto the line. The question is, how do you decide which axis the projection should be parallel to? – David K Jan 26 '19 at 20:57
  • Yeah. I meant I want the projection of P2. I have already fixed that in my question. Thanks. About your question. I'd say it needs to be the closest one. In the second example of the GIF, I could project left or down to be at the line. But it had to be left because it is closer. – Ramon Amorim Jan 26 '19 at 21:37

1 Answers1

1

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.

David K
  • 98,388