Find third point of a right triangle where the third point is on a line going through first point I need to figure out an equation that will be used in a graphical drawing tool, where each angle in the polygon will be 90deg. That is to say each new point added in the polygon will be forced so that it creates a 90deg angle. So this only applies when you have 2 points or more in the polygon being drawn.
I believe I could apply this logic for each new point in the polygon:
Given two points A and B, how can I find point C forming a right triangle (with 90deg angle at B), where point C is on the line formed by point A and another known point P (the mouse cursor point) which lies on line PA.
I went as far as calculate the slope of mAB and inverted slope mBC, but unsure how to proceed from here. Do I need to find the intersection point of the lines BC and PA?
What would be the equation to find point C?

First implementation of solution is wrong (wip)

Second implementation using Doug's value for t is correct. Accepted answer.

 A: From the picture, $AC$ is just a scalar multiple of segment $AP$, which is known.  That is
$$(C-A) = t(P-A),$$
where the subtraction is done component-wise.  We also know that $AB$ and $BC$ are orthogonal, so that
$$(B-A) \cdot (C-B) = 0.$$
You can treat this is a system of equations, where we want to  solve for $C = (c_x,c_y)$.
Expanding the above equations gives
$$t(p_x-a_x)+a_x = c_x,\quad t(p_y-a_y)+a_y = c_y,$$
$$(b_x-a_x)(c_x-b_x)+(b_y-a_y)(c_y-b_y) = 0.$$
Now, substitute $c_x$ and $c_y$ into the second equation.  Solve for the number $t$.  Then, point $C$ is given by
$C = (a_x+t(p_x-a_x),a_y+t(p_y-a_y))$.
$$t = \frac{(a_x-b_x)^2+(a_y-b_y)^2}{(b_x-a_x)(p_x-a_x)+(b_y-a_y)(p_y-a_y)}.$$
Notice that this answer is in agreement with Hosam's answer.
A: Find the angle between $AP$ and $AB$ using the formula
$ \theta = \cos^{-1} \left( \dfrac{ AP \cdot AB }{\| AP \| \| AB \| } \right)$
Then the distance $x$ from $A$ to $C$ is related to $\| AB\| $ and $\theta$ by
$ \cos( \theta) = \dfrac{ \| AB \| }{x } $
Hence,
$ x = \dfrac{ \| AB \|^2 \| AP \| }{ AP \cdot AB } $
Now point $C$ is along $AP$ but scaled up by a factor of $\dfrac{x}{\| AP \|} $
Therefore,
$\boxed{ C = A + \dfrac{ \| AB \|^2 }{AP \cdot AB} (AP) } $
So, for example, if $A = (-1, 0), B = (\dfrac{1}{2}, \dfrac{\sqrt{3}}{2} ), P = (-\dfrac{1}{2}, 0 ) $, then
$ AP \cdot AB = (\dfrac{1}{2}, 0) \cdot (\dfrac{3}{2}, \dfrac{\sqrt{3}}{2}) = \dfrac{3}{4} $
and
$\| AB \|^2 = \dfrac{9}{4} + \dfrac{3}{4} = 3 $
Hence,
$C =  A + \dfrac{ \| AB \|^2 }{AP \cdot AB} (AP) = (-1, 0) + 3\left(\dfrac{4}{3}\right) (\dfrac{1}{2}, 0) = (1, 0)$
