[Math] How to calculate the displacement between points

trigonometry

I'm having trouble finding the right formula for displacement between two points. I'm working on a program that will place a digital ruler and allow the user to trace their finger on the edge of the ruler and draw a line on the screen after they have positioned it to the position and rotation of their choosing. I have a problem though, because of my current formula (distance) I can only ever draw in a positive direction. If I try to draw in a negative direction, it will just draw in the positive direction again. For example, as the user pulls their finger to the left, the line gets longer to the right.

Of course I know that the reason is distance is scalar and can never be negative. Right now I have the starting touch point of the drag, the end touch point of the drag (where their finger is now) and the angle of the ruler. Distance is simply this ($p$ is touch start, $q$ is touch end, $r$ is actual end, $\Theta$ is the angle of the ruler):

$d =\sqrt{(q_{x}-p_{x})^{2} + (q_{y}-p_{y})^{2}}$

Then, the resulting straight line based on the angle of the ruler is:

$r_{x} = p_{x}+d*\cos \Theta$

$r_{y} = p_{y}+d*\sin \Theta$

Is there a formula for displacement instead of distance that I can use to replace $d$?

I will further clarify $\Theta$, it is the amount that the entire ruler has been rotated with regards to the screen. So the line from the start to end of the ruler will be covered by the above equations in the positive direction, but not in the negative (i.e. the user starts a line in the middle of the ruler and draws to the "left", the always positive nature of d ensures that it only grows to the "right").

Best Answer

It sounds as if you are miscomputing $\Theta$. If you compute $\Theta$ properly, the line should get longer to the left. To handle the quadrant of $\Theta$ simply, we can use $$ \tan\left(\frac\Theta2\right)=\frac{q_y-p_y}{d+q_x-p_x}\tag{1} $$ We still have to worry about the case when $d+q_x-p_x=0$, but unless $q=p$, that only happens when $q$ is directly to the left of $p$, that is, when $\Theta=\pi$ radians.

Since $\tan\left(\frac\Theta2\right)$ determines $\frac\Theta2$ up to a multiple of $\pi$ radians, $\Theta$ is determined up to a multiple of $2\pi$ radians. Therefore, the direction is fully determined by $(1)$.

See this answer for a justification of $(1)$.