0
$\begingroup$

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").

  • 0
    @borrrden: You may be able to ask a related question on Stack Overflow. You could tell which operating system you're using and which computer functions you're using. They should probably be able to tell you how to get another point. You can always come back here for mathematical formulas.2012-07-17

2 Answers 2

3

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)$.

  • 0
    I say "right to left" but I think you understand that it isn't always literal. At 90 degrees it means "top to bottom"2012-07-17
0

Alright, now that it is a new day I've got the criteria for negating the distance.

Let $\theta_{1}$ be the angle of the ruler (rotation in terms of the screen from the system) and $\theta_{2}$ be the angle of the user's drawn line (atan2($\Delta y, \Delta x)$ between start and end point). The displacement should be negative when $\|\theta_{2}-\theta_{1}\|>\frac{\pi}{2}$