### How to calculate a distance between points using vectors?

Hello, I have the following problem:
Entering coordinates of points on a plain with keyboard (as rows x,y). Analysing this coordinates. Replacing coordinates in vector (point - structure data type). Then calculating distance from the first point to all another points.

I've written the code of this problem using arrays -> formula r=sqrt( (x[i]-x[1])^2+(y[i]-y[1])^2) ), (coordinate plain).

 ``1234567891011121314151617181920212223242526272829303132`` `````` #include #include #include main() { float x[100], y[100]; int i, n; float r; cout << "Enter dimension of 1D massives (vectors of X & Y coordinates)\n"; cin >> n; cout << "Enter X coordinates of points for the 1st 1D massive (vector 1):\n"; for (i = 0; i < n; i++) { cin >> x[i]; } cout << "Enter Y coordinates of points for the 2nd 1D massive (vector 2):\n"; for (i = 0; i < n; i++) { cin >> y[i]; } for (i = 0; i < n; i++) { r=sqrt( ((x[i]-x[0])*(x[i]-x[0])+(y[i]-y[0])*(y[i]-y[0])) ); cout << "\n r = " << r; cout << "\n"; } return 0; }``````

How to write it using vectors?

 ``123456789101112131415161718192021222324252627`` `````` #include #include #include struct { { float x, y; } points; main() { using namespace std; int i; float r; cout << "Set dimension of vector"; cin >> i; for (i=0; i> points.x; cout << "Input y:"; cin >> points.y; } std:: vector pointvect; // vector declaration points points; pointvect.push_back (points); // add point to the vector r=/* ??? */``````

I think, iterators would be used here.
Last edited on
The pythagorean theorem works on any n-dimensional vector.

For example, given the 3D points: (a, b, c)1 and (a, b, c)2, the distance between them is:

d = √( (a2-a1)2 + (b2-b1)2 + (c2-c1)2 )

Hope this helps.
How to use iterators to find the distance?
Why do you want to use iterators? It makes no difference whether you do or don't.
After attending coordinates to the vector what should I do to find distance?
Maybe this?
 ``12345`` ``````vector::iterator pos; for (pos = pointvect.begin(); pos != pointvect.end(); pos++) { r=sqrt( (pointvect[i]-pointvect[0])*(pointvect[i]-pointvect[0])); cout << r;``````
Pay attention.

(1) Square each element.
(2) Sum the elements.
(3) Find the square root.
Why did you advice square? I've written the following code, it works but shows a distance equal to 0.0. Can anyone explain why?
 ``123456789101112131415161718192021222324252627282930313233343536`` `````` #include #include #include using std::cout; using std::cin; //2 dimensional vector struct struct points { float x, y; }; main() { int i; float r; std::vector pointvect; // vector declaration points * point; //declare pointer cout << "Set dimension of vector"; cin >> i; //with struct only 2D accepted for (i=0; i> point->x; //assign values cout << "Input y:"; cin >> point->y; pointvect.push_back(point); // add point to the vector } r=sqrt(((pointvect[i]->x - pointvect[0]->x)*(pointvect[i]->x - pointvect[0]->x)+(pointvect[i]->y-pointvect[0]->y)*(pointvect[i]->y-pointvect[0]->y) return 0; }``````

I'm confused about something. So do you want your program to list the distance between adjacent vectors?
Example:

If you type in 3 points (let's callt hem A, B, and C)
It would display 2 numbers

First would be distance from (A to B)
Second would be distance from (B to C)
am I correct in assuming this?

Edit: If that's what you wanted here was my approach, hopefully it makes sense. It appears that whoever taught you vectors didn't give you a clear understanding of it. Not sure why you were trying to use pointers when it's not necessary.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include #include #include using std::cout; using std::cin; //2 dimensional vector struct struct points { float x, y; }; int main() { int Dimensions=0; float r; std::vector pointvect; // vector declaration cout << "Set dimension of vector"; cin >> Dimensions; //with struct only 2D accepted for (int i=0; i> pointvect[i].x; //assign values cout << "Input y:"; cin >> pointvect[i].y; } for (int i=0;i
Last edited on
Thank you so much, very helpful. In this case problem can be solved by several ways. Exact wording of problem requires solving by dynamical arrays called "vectors". I agree that it makes better sense using statical arrays in that case.
The problem is that the word "vector" has multiple meanings. I mistook your meaning.

I think what you want to do is something like this?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778`` ``````#include #include #include #include #include #include #include using namespace std; struct point_t { float x, y; point_t(): x( 0.0f ), y( 0.0f ) { } point_t( float x, float y ): x( x ), y( y ) { } }; istream& operator >> ( istream& ins, point_t& point ) { ins >> ws; bool is_parens = ins.peek() == '('; if (is_parens) ins.get(); ins >> point.x; ins >> ws; if (ins.peek() == ',') ins.get(); ins >> point.y; if (is_parens) { ins >> ws; if (ins.peek() == ')') ins.get(); else ins.setstate( ios::failbit ); } return ins; } ostream& operator << ( ostream& outs, const point_t& point ) { return outs << "(" << point.x << "," << point.y << ")"; } float distance( const point_t& a, const point_t& b ) { return sqrt( (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y) ); } int main() { vector points; cout << "Enter as many (x,y) coordinate pairs as you would like.\n" "Press enter twice to finish.\n"; string s; while (getline( cin, s ) and !s.empty()) { istringstream ss( s ); point_t point; while (ss >> point) points.push_back( point ); } if (points.empty()) { cout << "You have not entered any points.\nGood-bye.\n"; return 0; } cout << "The distances between every point and the first are:\n"; for (auto& point : points) { cout << distance( points[ 0 ], point ) << "\n"; } cout << "\nGood-bye.\n"; }``````
This is screaming out for a vector class. If you create one, you could give it the required operators (in this case, the subtraction operator for sure) and a magnitude method.

Edit: A mathematical vector class, I mean.
Last edited on
Yes, algorithm is right but tools of your coding and coding of posts above are different (by used of set of instruments). Using definition of vector from vector algebra aren't always necessary.
Topic archived. No new replies allowed.