general formula for an orthogonal projection of a point onto a line

3.1k Views Asked by At

Could someone confirm this or correct the mistakes because this seems somehow wrong although I double checked it.

$(m_x,m_y)$ are coordinates of a point , $(p_x,p_y),(k_x,k_y)$ are coordinates of a line on a Cartesian plane, $(D_x,D_y)$ for projected point.

$$ D_x = (m_y-p_y+m_x*(k_x-p_x)/(p_y-k_y)-p_x*(p_y-k_y)/(p_x-k_x)) / ((k_x-p_x)/(p_y-k_y)-(p_y-k_y)/(p_x-k_x)), $$

$$ D_y = (Dx-p_x)*(p_y-k_y)/(p_x-k_x)+p_y.$$

1

There are 1 best solutions below

2
On

What a mess. Let's introduce notation for the slope of the given line, $s=(P_y-K_y)/(P_x-K_x)$. The your formula for $D_x$ is $$D_x = \frac{M_y-P_y - M_x/s-sP_x}{-s-1/s} \tag{1}$$ This is not what I get, though. The point $(D_x,D_y)$ must lie on the intersection of two lines: slope $s$ through $P$ and slope $-1/s$ through $M$. This gives $$D_y-P_y = s(D_x-P_x) \\ D_y-M_y=-\frac{1}{s}(D_x-M_x)$$ Subtract second from the first: $$M_y-P_y= -sP_x-\frac{1}{s}M_x +(s+1/s)D_x$$ Hence, $$D_x = \frac{M_y-P_y+sP_x+M_x/s}{s+1/s}\tag{2}$$ Similar to (1), but differs in the sign of $M_y-P_y$.

That said, I would not use the slope form myself: it creates problems when the given line is horizontal or vertical. I recommend vector projection instead.