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

  • 2
    But your angle $\Theta$ should actually indicate the direction. I don't understand your problem.2012-07-17
  • 1
    Well, the angles are supposed to allow for drawing even in a "negative direction"...2012-07-17
  • 0
    @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)2012-07-17
  • 0
    @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.2012-07-17
  • 0
    Yep, that's right.2012-07-17
  • 0
    @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?2012-07-17
  • 0
    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.2012-07-17
  • 0
    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.2012-07-17
  • 0
    @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.2012-07-17
  • 0
    How is the ruler oriented?2012-07-17
  • 0
    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.2012-07-17
  • 0
    Are you using the two-argument arctangent (`atan2()`) for your angles, or something else?2012-07-17
  • 0
    @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.2012-07-17
  • 0
    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.)2012-07-17
  • 0
    @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.2012-07-17
  • 0
    @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.2012-07-17
  • 0
    @MattGroff Yes, that is another possibility that I am considering. Calculating the 3rd point stumps me though.2012-07-17
  • 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