distance between point and line as a dot product?

62 Views Asked by At

I am doing the udacity course "AI for robotics" and in one video S. Thrun presents these formulas for calculating something called the cross-track error (cte). I understand where the formula for $u$ comes from, it is the magnitude of the projection of R into the line segment $\overline{\rm P_1P_2}$ (although I think it is missing a $sqrt()$ around the denominator). I however dont't understand where does the cte formula comes from. I was able to derive it in a different way:

$cte = \lvert\lvert R- Pu \rvert\rvert$ where $P$ is the vector from $P_1$ to $P_2$.

But why can we exchange the coordinates of $R$ flip the sign of $R_x$ and dot it with $P$ to get $cte$?. I understand that inverting the $x$ and $y$ coordinates of a $2D$ vector is equal to a reflection in the line $y=x$ but I am missing how does that relate to this problem. enter image description here