i have x and y coordinates that move around the screen randomly the position of the coordinates always vary they could suddenly stop or the movement speed could increase.

what i want is to find out at what speed the coordinates are traveling in miles per hour. (not necessarily miles per hour it could be 100 pixels per second)

any suggestions will be appreciated!!


Recommended Answers

All 16 Replies

You could use Euclidean distance to compute the speed. If you know the distance (from point A to point B) and the length of time it is moving, you should know how to compute the speed?

thanks for your reply.
thats a problem i am not sure of the distance that the coordinates are going to travel the imput i get for the coordinates is totally random and unpredictable. x and y values can increase, decrease or stop changing at any time.

i am not very sure what formula i can use to actually see the speed that the coordinates are moving in.
i am also not sure what the distance will be because the given coordinates move all around the screen.

thank you

I'm confused. Coordinates don't move, they'd be pointless if they did.
Do you mean you have moving points? If so, Taywin's suggestion is correct. If you have two points, A and B, you have the distance, by Euclid. If you have the time it takes to get from A to B, it's an easy calculation. If you don't have point B yet, obviously you can't calculate the rate yet.

sorry, i meant i have a point that moves around the screen, but there is no point b, its only point a that moves around. i need to work out the speed that it moves around the screen.

when the program executes the point a starts moving(randomly) it keeps on moving till the program is terminated. all i have to do is display the speed that the point is moving at.

i hope this makes it clearer
Thank you

Speed is distance over time. If a point moves, what you mean is it's at (X1,Y1) at time T1, and later it's at (X2, Y2) at time T2. So instead of saying points A and B, say A1 and A2 - same thing. (sqrt((X2-X1)^2 + (Y2-Y1)^2)))/(T2-T1)

thanks a lot for this formula, i am still not very sure would this work in my case.
in order to achieve what is shown in this formula would i have to store the (x1, y1) at the execution of the program and then use them as constant where as (x2, y2) would keep on changing, while the program is running?

and would T1 time would be stored when the program is executed and T2 time would be the current time?

Thanks again for your response

Actually the movement is from (x2,y2) to new (x2,y2) and divide by the time since your last update.

so does this mean i would have to store all the (x, y) to compare with the new (X,y)
my values keeps on changing all the time this would mean i would have to store 1000's of different coordinates...

thank you

This is a series of movements. Here, here's some examples to make it easier:

Imagine a car on a long straight highway, say from Portland to Seattle. (we'll call the highway a straight line to make it easy). The driver wants to know how fast he went. If he just wants to know his average speed for the trip, he can start a clock when he leaves Portland and check the total elapsed time when he gets to Seattle. He takes the distance travelled from his odometer, and divides it by time, and he has his average speed.
Now suppose he wants to know how fast he went on various segments of the journey. He makes a gadget that has a button on it, each time he pushes the button, it records the time and the odometer reading. Now he can calculate the delta-T and delta-D for each segment and calculate his average speed from Portland to Olympia and from Olympia to Tacoma and from Tacoma in to Seattle. Total average speed will still be the same, but he has more precise data for the segments.

Okay, now the same guy wants to go for a hike, on a loop trail. He has a GPS device, and he wants to know his average speed on the hike. If he just pushes the button at the start and the end of the hike, his total distance will be zero - x2-x1 = 0, y2-y1=0, x2=x1 and y2=y1 - so he's not getting any data.
So he modifies his dashboard doohickey to register his GPS coordinates and time each time he pushes the button. Now he can get a series of points (like in the second example above) and now he can get a approximation of his average speed as well as his speed on any given segment. The more times he pushes the button - the more points he registers - the more accurate his final average will be. (suppose he's walking up a series of switchbacks - the speed he registers will depend largely on what points he chooses to push the button at)

So for your moving point, you're like the hiker on the trail. You're moving from point to point in a 2-dimensional space, and by registering x,y,and t in some list you can calculate a series of velocities. The higher your sampling rate, within reason, the more accurate your overall average will be, but you can minimize the required sampling and improve your accuracy by being smart about when you sample. For example, if the guy walking up the switchbacks takes a sample at each curve, he'll get a good average. If he takes a sample at every other curve, he'll get a terrible average (because he'll walk a long way to get only a few feet up the hill). So if you sample, for example, each time your point changes direction, you'll get a better result than just sampling on some fixed time slice.

I hope some of this helps.

thank you for such a detailed explanation. this does help me to understand the theory of how the speed can be calculated but it also totally confuses me on how it could be implemented in to the code...
i think i could store (x, y) every second , where in one second there can be ten different occurrences of (x,y) and i could find average speed of the (x,y) that happened a second from (x,y) that is currently outputting.
am i on the right track??

thank you once again

this does help me to understand the theory of how the speed can be calculated but it also totally confuses me on how it could be implemented in to the code

Sorry about that...

I think you're on the right track. I'm a little confused by "store (x,y) every second)" and "ten different occurrences of (x,y)" in a second. If you have ten discrete occurrences of (x,y), why not store them?

Picture a flea on a floor. The flea jumps from time to time, and each landing point is a data point. Assume its direction and distance of jumps are completely random. If you take every tenth jump as a data point, what kind of a representation will you have of his progress? You can model this with a piece of graph paper and a pencil - each jump is a change of some random number [-3..+3] to x and y. Make 50 such moves and then plot every tenth one - does taking every tenth move make a good representation? What if the jumps were bigger, would that change anything?

As I say, I think you'd probably do best to log each point.

once again thanks for your replay,
i still cant imagine how this could be implemented in to the code...by any chance you could show me pseudo code example or just Java code.

i would be really grateful , i have been struggling with this speed issue for quite a while and it is starting to become frustrating

Thank You Very Much

Well, here's a sketch, off the top of my head. I may have made some errors in here, but I don't have time to be more careful. It'll give you an idea, in any case:

One thing you could do would be to create a class "TimePoint" which would simply keep a Point and a timestamp.

Constructor would take a point and call System.getTimeMillis() for the timestamp.

Make an arraylist of TimePoints.

Now, each time your magic jumping bean jumps to some Point p, add a new TimePoint(p) to your arraylist. Okay, now you have a log of the movements of your point.

To calculate velocity from one point to its successor, use Euclid as described above to get the distance, then divide by difference in times.
Probably easiest to use an ordinary for loop for this:

declare a double totalVelocity, set it to 0
declare a TimePoint, called oldTimePoint, set it equal to the first member of the list
for (i = 1; i <arraylist.size(); i++){  //notice 1 as starting index
  make a new TimePoint, call it newTimePoint. Set it to arrayList.get(i)
  do the calculation: deltaX = newTimePoint.getX()-oldTimePoint.getX(); etc
  totalVelocity +=result of calculation
  oldTimePoint = newTimePoint // set up the next iteration

Then divide totalVelocity by number of jumps to get average velocity per jump- notice that number of jumps is not the same as number of points. (this is a classic opportunity for a fencepost error)

That's roughly it, anyway.

This looks like a case of unclear spec to me.
"what i want is to find out at what speed the coordinates are traveling"
is that average speed from the start until now? the instantaneous speed (speed of latest jump)?, smoothed average recent speed?
Even if it's all three cases I can't see any need to have anything more than the initial position/timestamp, the current position/timestamp, and the most recent previous position/timestamp.

Probably true, that. Depends what you're looking for.
The pseudocode is pretty much a translation of the hiking example - if you don't need to keep the data, and you want to do the calculations on the fly, you can obviously simplify things.
Thanks for pointing that out, James.

problems isn't calculate some axe and coordinations between (lots or two) points, don't forget for acceleration or deceleration (linear/exponencial)

best of from OP would be to write some GUI and calculate outPut from mouseMovements ....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.