I am trying to calculate the distance between coordinates. I am detecting the movement from camera/video (using for tennis ball tracking/ OpenCV library) and saving the coordinates of all candidates so that means the ball movement and also the player or whatever noise there could be.
I store the coordinates in vector of points and also writing them to an excel file. So now I have my excel file with the 1 column = frame count, 2 column = number of candidates on that frame, and after that all the x and y coordinates.
QUESTION: What I want to do is calculate the distance of each point with all the points in the next frame. Let's say my excel with coordinates looks like that:
frame nr candidates X Y X Y X Y X Y 1 3 x1 y1 x2 y2 x3 y3 2 4 x5 y5 x6 y6 x7 y7 x8 y8 3 1 x9 y9 4 2 x10 y10 x11 y12 Now I want to calculate all the distances so the x1,y1 and x5,y5 distance, x1,y1 and x6,y6. x1,y1 and x7,y7. x1,y1 and x8,y8 then x2,y2 and x5,y5 and so on. So if i have 3 candidates in the 1st frame and 4 on the 2nd frame I would need to get 12 distances. Then the same thing with 2nd and 3rd : x5,y5 and x9,y9 then x6,y6 and x9,y9... 3rd and 4th and so on.... d = sqrt((x2-x1)^2 + (y2-y1)^2) is the formula that I want to use. How could i do this? I know how could I do it if I had only 2 points, found an example code see below, but when i have a lot of coordinates how could i accomplish that?
#include <stdio.h> #include <iostream> #include <vector> using std::cout; using std::cin; //2 dimensional vector struct struct points { float x, y; }; int main() { int Dimensions = 0; float r; std::vector<points> pointvect; // vector declaration cout << "Set dimension of vector"; cin >> Dimensions; //with struct only 2D accepted for (int i = 0; i<Dimensions; i++) { // Input coordinates x, ye pointvect.resize(pointvect.size() + 1); //This will make pointvect be able to hold 1 more point at the end cout << "Input x:"; cin >> pointvect[i].x; //assign values cout << "Input y:"; cin >> pointvect[i].y; } for (int i = 0; i<pointvect.size() - 1; i++) { cout << "Distance between " << pointvect[i].x << "," << pointvect[i].y << " and " << pointvect[i + 1].x << "," << pointvect[i + 1].y << std::endl; //Pythagorean Theorem (also used to calculate distance can be found by taking sqrt ( (y2 - y1)^2 + (x2-x1)^2 ) float Distance = sqrt(pow((pointvect[i].y - pointvect[i + 1].y), 2) + pow((pointvect[i].x - pointvect[i + 1].x), 2)); cout << "Distance:" << Distance << std::endl; } system("pause"); return 0; } So i hope my question is understandable. Thank you in advance!
for (int i = 0; i<pointvect.size() - 1; i++)...float Distance = sqrt(pow((pointvect[i].y - pointvect[i + 1].y), 2) + pow((pointvect[i].x - pointvect[i + 1].x), 2));You are looping up tosize() - 1, but accessing an element atvector[size](see thei + 1index).