0

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

borrrden
  • 101
  • 2
    But your angle $\Theta$ should actually indicate the direction. I don't understand your problem. – Raskolnikov Jul 17 '12 at 09:00
  • 1
    Well, the angles are supposed to allow for drawing even in a "negative direction"... – J. M. ain't a mathematician Jul 17 '12 at 09: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 09: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 09:04
  • Yep, that's right. – Raskolnikov Jul 17 '12 at 09:05
  • @Raskolnikov How do I know when to change it though? Do I have to check which quadrant the angle is in and then say "if next x is less than start x and next y is less than start y and angle is in quadrant 1" etc? – borrrden Jul 17 '12 at 09:06
  • I'm not really sure where your x and y are coming from if we're speaking about angles and distances. I think you're making things awfully complicated. If you already dispose of coordinates, there's no need to switch to angles and distances. Just subtract the p and q coordinates and rescale properly to get your r. – Raskolnikov Jul 17 '12 at 09:09
  • By the way, do you realize that your formulas for the r coordinate should give you exactly the same as the q coordinate if they work properly? That's what the mathematical formula you put down implies, at least if $\Theta$ is defined properly. – Raskolnikov Jul 17 '12 at 09:11
  • @Raskolnikov P is the first position that a user touched, and Q is the current position where the user dragged to. I need to draw the line on the ruler, regardless of where the user's finger goes (that's where the distance comes in). I need to stay on the edge of the ruler, so that's where the angle comes in. So I need the resulting R that would be the end of the line on the ruler with the same distance as the user dragged. However, if they drag back then they could go past the initial P in the negative direction, so I need to know how to handle that. – borrrden Jul 17 '12 at 09:15
  • How is the ruler oriented? – Raskolnikov Jul 17 '12 at 09:18
  • The ruler is as if flat on a table, rotated to a theta according to the user (using two fingers to spin it about one axis). The theta value is provided in radians by the OS. – borrrden Jul 17 '12 at 09:20
  • Are you using the two-argument arctangent (atan2()) for your angles, or something else? – J. M. ain't a mathematician Jul 17 '12 at 09:22
  • @J.M. The angle of the object is provided by the operating system. It is correct as you would look at it from the top spinning on its Z-axis. – borrrden Jul 17 '12 at 09:26
  • Huh. What system/language is this? Is your "operating system" able to return obtuse angles? (That's why you're not supposed to be worried about $d$ being positive.) – J. M. ain't a mathematician Jul 17 '12 at 09:28
  • @J.M. This is iOS (Apple's mobile operating system). The angle is the angle that the entire ruler has been rotated. So the ruler will look like this - at 0 degrees, | this at 90 degrees, etc. but if you start drawing in the middle, it is either acute or obtuse depending on which way you move your finger. On the 90 degree example, down is 90 but up is 270. The orientation of the ruler doesn't change as the user draws on it though so that is where I am getting stuck. – borrrden Jul 17 '12 at 09:31
  • @borrrden: Is there any possibility of using more points? It seems that this might have to do with the behavior of the ruler, and not the math. It seems like you could use a total of 3 points. You use two points to draw the ruler. Then the user clicks the mouse button. Then a third point is used to determine the distance along the ruler. This could be done mathematically, and would solve all of your orientation problems. So I'm thinking that this all has to do with how the ruler and the computer behaves. – Matt Groff Jul 17 '12 at 10:04
  • @MattGroff Yes, that is another possibility that I am considering. Calculating the 3rd point stumps me though. – borrrden Jul 17 '12 at 10:10
  • @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. – Matt Groff Jul 17 '12 at 10:25

2 Answers2

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

robjohn
  • 345,667
  • $\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 09: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 09: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 09:39
  • Yeah, the two-argument arctangent might be a better thing to use than the angle he's currently getting... – J. M. ain't a mathematician Jul 17 '12 at 09: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 09:43
  • @borrrden: from what you have said in your question, you want to compute $d$ and $\Theta$ so that the formulas for $r$ return the proper value. Since you said that everything draws to the right, it seems that the value of $\Theta$ is being restricted to quadrants I and IV, which is the standard output of the $\arctan$ function. The problem is not with $d$, but with $\Theta$, and formula $(1)$ is an attempt to get the proper value of $\Theta$. – robjohn Jul 17 '12 at 09:48
  • @borrrden: which equation do you think would be reversed? Equation $(1)$? $\arctan\left(\frac{q_y-p_y}{q_x-p_x}\right)$? If $\Theta$ is off by $180^\circ$, then both $x$ and $y$ are negated. – robjohn Jul 17 '12 at 09:50
  • Ah, I think this is the part that I failed to explain properly. Theta doesn't change while the user draws. It is set by the user before they start drawing and it is defined as the delta angle between the screen and the ruler. So for a left to right line, the angle will equal the ruler's angle, but for a right to left line it will be offset by 180. The line can only go on either of these two angles, regardless of how badly the user strays off the ruler path. I think for now I will just store the quadrant of the angle when the user finishes rotating and then check similar to what you posted – borrrden Jul 17 '12 at 10:01
  • I say "right to left" but I think you understand that it isn't always literal. At 90 degrees it means "top to bottom" – borrrden Jul 17 '12 at 10:01
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}$

borrrden
  • 101