In: Computer Science
Given the definition for a Point class that holds the coordinates of the point as double values x and y, write a function called pt_dist that takes two points and returns the straight-line distance between them (as a double). Use two ways, pt_dist function version and the pt_dist method version. In main, include two if else tests for each, If passed "TEST PASSED, DIST IS " else "Test Failed, dist is ".
Hint: Rhymes with Bythagorean Beorem.
#include <iostream>
#include <cmath>
using namespace std;
// given point class
class Point
{
public:
double x, y;
};
// put your function here, and name it pt_dist
//method version
int main() {
return 0;
}
HI, Please find my implementation
Please let me know in case of any issue.
#include <iostream>
#include <cmath>
using namespace std;
// given point class
class Point
{
public:
double x, y;
// method declaration
double pt_dist(Point &other);
};
// put your function here, and name it pt_dist
double pt_dist(Point &first, Point &second){
return sqrt((first.x - second.x)*(first.x -
second.x) + (first.y - second.y)*(first.y - second.y));
}
//method version
double Point::pt_dist(Point &other){
return sqrt((other.x - x)*(other.x - x) +
(other.y - y)*(other.y - y));
}
int main() {
// creating two objects
Point p1;
p1.x = 3;
p1.y = 4;
Point p2;
p2.x = 8;
p2.y = 9;
double dist1 = p1.pt_dist(p2);
double dist2 = pt_dist(p1, p2);
cout<<"Distance:
"<<dist1<<endl;
if(dist1 == dist2){
cout<<"TEST PASSED, DIST
IS"<<endl;
}
else{
cout<<"TEST Failed, DIST
IS"<<endl;
}
return 0;
}