Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this problem. I used a vector to store the x and y coordinates of the mouse. Basically, my objective is to find the difference between the new x positions from the previous x positions of the mouse.How can i do that?

This is the program that i have been using:-

C++
vector<Point>vec(0);
for(int m = 0;m<10;m++)
{
vec.push_back(Point(cursor.x,cursor.y));
cout<<"The mouse positions are:"<<vec[m]<<endl;
}
Posted
Updated 16-Sep-14 12:36pm
v2
Comments
Philippe Mori 16-Sep-14 19:39pm    
If you do that, chances are all 10 points will be exactly the same.
Sergey Alexandrovich Kryukov 16-Sep-14 19:44pm    
:-)
I credited your note in my answer. :-)
—SA
Rebecca1995 16-Sep-14 20:02pm    
@Phillppe Mori:- Thanks for your reply.I am getting different points and i am moving the mouse using a colored object. I am sorry that i dint expect the whole program in details. I am using a colored object detection principle to move the mouse and i can see that i am getting different points as i move my object(indirectly my mouse).
Sergey Alexandrovich Kryukov 16-Sep-14 20:11pm    
All right, but you cannot do it in a loop just like that. You need to handle the event when mouse is really moved. For Windows OS, it would be WM_MOUSEMOVE...
—SA

1 solution

You already know how to access elements of the vector; we can see it from your code — using the operator '[]'. As to the distance, this is the distance in the metric space, in this case — Euclidean:
http://en.wikipedia.org/wiki/Euclidean_space#Distance[^],
http://en.wikipedia.org/wiki/Euclidean_distance[^].

Which part of it could be a problem?

[EDIT]

And did you get the comment to the question by Philippe Mori. He is right: you are taking the mouse location well too fast, event when the location is not changed. This is completely pointless. You need to do it in the handler of the WM_MOUSEMOVE Windows message (for Windows OS). How to handle it, depends on the UI library you are using; you can do it even using raw Windows API.

—SA
 
Share this answer
 
v4
Comments
Rebecca1995 16-Sep-14 19:53pm    
@Sergey Alexandrovich:- Thanks for your reply. I am still a bit confused. So, say i have vec[0] = [200,250} and vec[1] = [250,250]. Intuitively, the difference in x positions will be (250-200) = 50 in the cartesian plane. Now, i want this to be calculated automatically in my loop and i have to access the x elements of my vector for that somehow. How do i do that?
Sergey Alexandrovich Kryukov 16-Sep-14 20:08pm    
Intuitively? Sure. How about strictly? This is elementary mathematics, in essence: square root of the squares of distances in each direction.
If you know one distance, what's the difference if you need N distances. The question is: distances between which points, and why? Or do you need the path length? How about thinking just a bit?
—SA
Rebecca1995 16-Sep-14 20:03pm    
@Sergey Alexandrovich:- I answered his comment. Thanks
Sergey Alexandrovich Kryukov 16-Sep-14 20:09pm    
Thank you.
—SA
Rebecca1995 16-Sep-14 20:18pm    
@Sergey Alexandrovich:- Thanks for your reply. I understood your point, and i get it that the distance d can be found using the distance formula. To be honest i need to determine the object trajectory(such as linear/curve etc). I have the points now and i need a way to determine the trajectory.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900