# Thread: calculating the speed of moving point

1. Member
Join Date
Feb 2010
Posts
51
Rep Power
0

## calculating the speed of moving point

hey,

i receive one set of x and y coordinates. the coordinates keeps on changing randomly. the x could increase and y could decrease or vise versa.
the x and y values could stop changing suddenly.
the x and y values could start increasing/decreasing very fast or very slowly.
the x value could increase/decrease but y value could stay the same or vise versa.

the input of x and y is received when the program is executed and the new values of x and y are fed to the program while the program is executing.

what i want is to calculate the speed of that moving point(x,y) , in pixels per second.

so if the point is not moving( x and y values do not change ) the speed should be 0

if the x and y values increase/decrees very fast the speed should change accordingly e.g 1000 pixels/sec

it should work in the similar concept as the car, once the driver sits in the car and starts the engine and if he or she does not know where and how far they going the speedometer would still be showing the speed at which the car is going

i do not know what distance the x and y will cover and i do not know the time for how long the program will be executed and how long the x and y values will be changing

i hope you understand what i need and will be able to help me.

thank you very much

any help will be greatly appreciated

2. Do you know the distance formula? Just use it to determine distance each step, then take the average.

3. Member
Join Date
Feb 2010
Posts
51
Rep Power
0
well i do know distance formula it would be speed = distance/time;
but as i said i do not know the distance and i do not know the time.
and i do not get any different stages in the program my x y moves all around the screen or just stops.

thanks

4. Senior Member
Join Date
Jan 2009
Posts
671
Rep Power
10
Speed is distance/time. To calculate the distance between two points,

Java Code:
```double delX = x2 - x1;
double delY = y2 - y1;
double d = Math.sqrt(delX*delX+delY*delY);```
Beyond that, you have to decide if you want to report instantaneous speed or some form of predictive or averaged speed. Those are algorithm choices rather than java specific issues.

5. The distance formula is not distance/time. That would not make sense. I meant the point distance formula.

I'm not really sure what you're saying about not getting different stages. If you want help with code, you'll have to post an SSCCE that demonstrates what you're talking about.

PS- You really shouldn't post duplicate threads. It's really confusing to follow.

6. Member
Join Date
Feb 2010
Posts
51
Rep Power
0
thanks for this formula, but what would be the x2 and y2 i understand x1 and y1 is the current position of my point.

if x2 and y2 is the previous position, but which position would it be and when.

the coordinates change all the time think of it as a mouse cursor if you move it the speed of it would change according how fast you moving it and if you stop the speed should be zero.

mouse cursor does not have starting or ending position it just moves when you tell it to move and moves as long as you keep on instructing it to move. so how would the speed of it would be calculated

thank you

7. Originally Posted by gedas
thanks for this formula, but what would be the x2 and y2 i understand x1 and y1 is the current position of my point.

if x2 and y2 is the previous position, but which position would it be and when.

the coordinates change all the time think of it as a mouse cursor if you move it the speed of it would change according how fast you moving it and if you stop the speed should be zero.

mouse cursor does not have starting or ending position it just moves when you tell it to move and moves as long as you keep on instructing it to move. so how would the speed of it would be calculated

thank you
Sample your (x, y) points at regular time interval lengths t. Make t is small as possible.

kind regards,

Jos

8. Member
Join Date
Feb 2010
Posts
51
Rep Power
0
sorry for double posting , new to this forum...

so if i store the coordinates lets say every 100 milliseconds. then i find the distance between the coordinates that i stored 100 milliseconds ago and now. so i have my distance which i would divide by 100 milliseconds this would have to show me the speed right?
am i on the right track?

thanks again

9. Senior Member
Join Date
Jan 2009
Posts
671
Rep Power
10
Yes, that's correct. The disadvantage of sampling fast is that you will end up with limited resolution. For example, if you are sampling so fast that there is never more than 1 pixel of motion, then your speed is going to alternate between 0 and 1/time_delta...which is probably not what you want.

What you're trying to do can be algorithmically challenging, because what you probably really want is a Kalman filter that is tolerant of noise and predicts what the speed is *now* instead of reporting what it *was*.

Best of luck!

10. Two similar threads merged together. Original poster, please don't double post the same question in multiple threads.

11. Member
Join Date
Feb 2010
Posts
51
Rep Power
0
yeah thats a problem it gives me the speed that *was* instead of what it is now. also the problem with the above solution is that if small range of coordinates has been covered very fast it would still be divided just after 100 miliseconds and it still would give me the avrage speed instead of the current speed.

Kalman filter that is tolerant of noise and predicts what the speed is *now* instead of reporting what it *was*.

thank you very much

Kalman Filter

db

13. Member
Join Date
Feb 2010
Posts
51
Rep Power
0
well i read about it but i am still very puzzled how could this be implemented in the code. and the theory of it is too hard to grasp for me cos my math knowledge is limited to my high school education.

could you please explain to me how Kalman filter could be used in my situation.

thank you very much

14. Senior Member
Join Date
Jan 2009
Posts
671
Rep Power
10
A Kalman filter is a statistical predictor. It's implementation is a signal processing question rather than a Java question.

15. Member
Join Date
Feb 2010
Posts
51
Rep Power
0
well it is a question of how to implement signal processing in to java code. if you know what forum could answer me this question please let me know.
p.s my orginal question was not related with Kalman filter.

thank you

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•