Need to know if an object moved, from x1 to x2, for how much and in which direction
For our case say that y = 0.
examples
The simplest way to calc that ?Code:x1 x2 expected result 1 1 2 1 2 1 1 2 1 1 3 2 6 7 1 7 6 1 etc

Need to know if an object moved, from x1 to x2, for how much and in which direction
For our case say that y = 0.
examples
The simplest way to calc that ?Code:x1 x2 expected result 1 1 2 1 2 1 1 2 1 1 3 2 6 7 1 7 6 1 etc
Last edited by BubikolRamios; 05302011 at 02:02 PM.
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search
Well if y is always zero, then the distance traveled will always be x_final  x_initial. The sign of the answer will tell you whether your moved in the +x direction or the x direction.
If your y changes, however, you'll have to use the distance formula to calculate the MAGNITUDE of the distance. To get the direction and magnitude, I would use vectors, and their respective operations.
If both your Y and X coordinates change, you can just use the Pythagorean theorem (a2 + b2 = c2) where the difference in X is your a and the difference in Y is your b. Then solve for c and you have your distance between the two points.
The distance between Point 1 and Point 2 is 5.Code:Point 1 (1, 0) Point 2 (2, 4) 1  2 = a = 3 0  4 = b = 4 a squared = 9 b squared = 16 9 + 16 = c = 25 The square root of 25 = 5
deltaX = x2  x1 where deltaX is the distance moved on the x axis.
Bear in mind that subtracting a negative number is the same as adding the absolute value of that number.
If you take the highlighted xample:
deltaX = x2  x1 = 3  (1) = 3 + 1 = 2
If deltaX is negative (less than 0), the object moved in the negative direction on the x axis. If deltaX is positive, the object moved in the positive direction on the x axis.
If the object moves on the y axis as well, then as bobleny described, you can use Pythagoras' Theorem to solve for the straight line distance between 2 points.
distance = sqrt(deltaX^2 + deltaY^2)
In bobleny's example there needs to be a minor correction
Code:9 + 16 = c^2 = 25
Last edited by bullant; 05312011 at 02:10 AM.