Question
Point class contains X and Y member variables that store the coordinates of a point. Given n points as input, you are going to populate
Point class contains X and Y member variables that store the coordinates of a point. Given n points as input, you are going to populate a 2D dynamic array that each element of the array determines the Euclidean distance between the corresponding points. GetPointsFromFilefunction reads a file containing a list of 2D points and returns a vector of points. ToDynamicArray function passes in a vector of point and returns a pointer to dynamic array containing the points of the vector. ComputeDistances passes in a pointer to dynamic array and computes a pairwise distances between the points and returns a pointer to 2D array that stores all the distances. Finally, DisplayDistances function outputs the distances to the console.
If the input file contains four points as following (each line stores X and Y values for a point separated by a single space):
0.0 5.0 5.0 0.0 10.0 12.0 3.0 4.5
Following distances will be printed to the console:
0 7.07107 12.2066 3.04138 7.07107 0 13 4.92443 12.2066 13 0 10.2591 3.04138 4.92443 10.2591 0
As an instance, the distance between P1(0.0, 5.0) and P4(3.0, 4.5) is computed as 3.04138.
First familiarize yourself with member variables and functions of the Point class as provided here:
Point.h
#ifndef _POINT_H_ #define _POINT_H_ #includeclass Point{ private: double X; double Y; public: Point(); Point(double,double); double Distance(const Point&)const; double getX(); double getY(); }; #endif
Point.cpp
#include "Point.h" Point::Point():X(0),Y(0){} Point::Point(double _x, double _y):X(_x),Y(_y){} double Point::Distance(const Point &p)const { return sqrt(pow((X - p.X),2) + pow((Y - p.Y),2)); } double Point::getX() {return X;} double Point::getY() {return Y;}
Also, familiarize yourself with following functions in main.cpp: GetPointsFromFile, DisplayDistances and main.
main.cpp
#include#include "Point.h" #include #include #include #include #include #include using namespace std; double** ComputeDistances(const Point*, int); vector GetPointsFromFile(string&); Point* ToDynamicArray(vector ); void DisplayDistances(double**, int); int main() { string filePoint; cout << "Enter the name of point file:" << endl; cin >> filePoint; vector pts = GetPointsFromFile(filePoint); Point* dpts = ToDynamicArray(pts); double** distances = ComputeDistances(dpts, pts.size()); DisplayDistances(distances, pts.size()); return 0; } vector GetPointsFromFile(string& filename){ ifstream inFS(filename.c_str()); if (!inFS.is_open()) { cout << "Error: can not open the file point." << endl; exit(1); } vector pts; double X; double Y; while(inFS >> X) { inFS >> Y; Point newP = Point(X,Y); pts.push_back(newP); } return pts; } void DisplayDistances(double** distances, int size){ for(unsigned int r = 0; r < size; r++){ for(unsigned int c = 0; c < size; c++){ cout << setw(10) << left << distances[r][c]; } if(r != size - 1) cout << endl; } }
You are to implement two functions for this lab:
Point* ToDynamicArray(vector
double** ComputeDistances(const Point* pts, int size): Passes in 1D dynamic array of points and returns 2D dynamic array of distances.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started