Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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

share|improve this question
2  
But your angle $\Theta$ should actually indicate the direction. I don't understand your problem. –  Raskolnikov Jul 17 '12 at 9:00
1  
Well, the angles are supposed to allow for drawing even in a "negative direction"... –  J. M. Jul 17 '12 at 9:01
    
@Raskolnikov suppose the angle of the ruler is 35, and the user starts in the middle. If they draw to the right it should be 35 degrees, but if they draw to the left it should be 215 degrees (or 35 degrees with a negative value for d) –  borrrden Jul 17 '12 at 9:03
    
@J.M. Yes, I'm sure if I had a negative value for d, it would draw correctly but I don't know how to get that value. –  borrrden Jul 17 '12 at 9:04
    
Yep, that's right. –  Raskolnikov Jul 17 '12 at 9:05

2 Answers 2

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

share|improve this answer
    
$\Theta$ is not a value that I compute myself. It comes from the operating system as a result of the user spinning the ruler with their fingers. –  borrrden Jul 17 '12 at 9:28
    
If you have $q$ and $p$, you should compute $\Theta$ yourself (using $(1)$) rather than relying on the OS, which seems to be returning the wrong value (probably $\arctan\left(\frac{q_y-p_y}{q_x-p_x}\right)$). If that is not practical, then you should add $\pi$ radians, or $180^\circ$, to $\Theta$ if $q_x<p_x$. That should correct for the $\arctan$ computation that I have assumed above. –  robjohn Jul 17 '12 at 9:36
    
$q$ is not as relevant to the equation as you might think. It is the touch value from the user, but I can't use it directly or they would have to draw a perfectly straight line which would defeat the purpose of the ruler tool. I use it as a hint to judge how much of my resulting line to draw (i.e. the line that goes perfectly along the ruler's edge whether or not the user has shaky hands, etc) –  borrrden Jul 17 '12 at 9:39
    
Yeah, the two-argument arctangent might be a better thing to use than the angle he's currently getting... –  J. M. Jul 17 '12 at 9:40
    
If the ruler is upside down though (rotated 180 degrees), then wouldn't your last equation be reversed? I'd want to add 180 if $q_{x}>p_{x}$ and for 90 degrees the x value should not change at all. Or am I mistaken? –  borrrden Jul 17 '12 at 9:43

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}$

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.