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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include <stdio.h>
  #include <iostream.h>
  #include <math.h>

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include <stdio.h>
  #include <iostream.h>
  #include <vector.h>

    struct {
{ float x, y;
  } points;

main()
{ using namespace std;

  int i; 
  float r;

  cout << "Set dimension of vector";
  cin >> i;
  for (i=0; i<i; i++) {  // Input coordinates x, y
    cout << "Input x:";
    cin >> points.x;
    cout << "Input y:";
    cin >> points.y;
}
  std:: vector<points> 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?
1
2
3
4
5
vector<points>::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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    #include <stdio.h>
    #include <iostream.h>
    #include <vector.h>

    using std::cout;
    using std::cin;

    //2 dimensional vector struct
    struct points
    {
    float x, y;
    };

    main()
    {
    int i;
    float r;
    std::vector<points*> pointvect; // vector declaration
    points * point; //declare pointer

      cout << "Set dimension of vector";
      cin >> i; //with struct only 2D accepted
    for (i=0; i<i; i++)
    { // Input coordinates x, y
      point = new point; //init a new points
      cout << "Input x:";
      cin >> 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#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, y
    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;
}
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <algorithm>
#include <ciso646>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
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 <point_t> 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.