Question

In: Computer Science

Given the definition for a Point class that holds the coordinates of the point as double...

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;
}

Solutions

Expert Solution

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;
}


Related Solutions

in C++ Write a definition for a class ‘point’ to describe a point with coordinates (X,Y)...
in C++ Write a definition for a class ‘point’ to describe a point with coordinates (X,Y) in 2-D space. The private data members are the coordinates X and Y of type ‘double’. The public member functions are: A default constructor (no parameters) to initialize X and Y to zero. An explicit value constructor with two parameters to initialize the values of X and Y. Two functions, one to set the X value and the other to set the Y value...
public class Point { protected double x, y; // coordinates of the Point //Default constructor public...
public class Point { protected double x, y; // coordinates of the Point //Default constructor public Point() { setPoint( 0, 0 ); } //Constructor with parameters public Point(double xValue, double yValue ) { setPoint(xValue, yValue ); } // set x and y coordinates of Point public void setPoint(double xValue, double yValue ) { x = xValue; y = yValue; } // get x coordinate public double getX() { return x; } // get y coordinate public double getY() { return...
A incomplete definition of a class Temperature is given below: public class Temperature { private double...
A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3}; } [6] (i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of...
Plot the point whose polar coordinates are given. Then find the Cartesian coordinates of the point...
Plot the point whose polar coordinates are given. Then find the Cartesian coordinates of the point b. (2, π/4) c.(−3, −π/6)
The Cartesian coordinates of a point are given. (a) (2, −5) (i) Find polar coordinates (r,...
The Cartesian coordinates of a point are given. (a) (2, −5) (i) Find polar coordinates (r, θ) of the point, where r > 0 and 0 ≤ θ < 2π. (r, θ) = (ii) Find polar coordinates (r, θ) of the point, where r < 0 and 0 ≤ θ < 2π. (r, θ) = (b) (-2, −2) (i) Find polar coordinates (r, θ) of the point, where r > 0 and 0 ≤ θ < 2π. (r, θ) =...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:  ...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:   The data fields x and y that represent the coordinates with gette and setter methods. A no-arg constructor that creates a point (0, 0).   A constructor that constructs a point with specified coordinates. The method equals(GeoPoint p) that returns true if two GeoPoint objects have the same x- and y-coordinates. Write a test program that creates an array of GeoPoint objects. The size of...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should contain: Two data fields x and y that represent the coordinates. A no-arg constructor that creates a point at (0, 0). A constructor that creates a point with specified coordinates. Get methods for data fields x and y respectively. A method named distance that returns the distance from this point to another point with specified x- and y-coordinates. Use the formula: root (x2-x1)2 +...
The Cartesian coordinates of a point are given. (a)    (−3, 3) (i) Find polar coordinates (r, θ)...
The Cartesian coordinates of a point are given. (a)    (−3, 3) (i) Find polar coordinates (r, θ) of the point, where r > 0 and 0 ≤ θ < 2π. (r, θ) = (ii) Find polar coordinates (r, θ) of the point, where r < 0 and 0 ≤ θ < 2π. (r, θ) = b. (5,5sqrt(3)) (i) Find polar coordinates (r, θ) of the point, where r > 0 and 0 ≤ θ < 2π. (r, θ) = (ii) Find...
For this task you will create a Point3D class to represent a point that has coordinates...
For this task you will create a Point3D class to represent a point that has coordinates in three dimensions labeled x, y and z. You will then use the class to perform some calculations on an array of these points. You need to draw a UML diagram for the class (Point3D) and then implement the class. The Point3D class will have the following state and functionality: Three data fields, x, y and z, of type double, represent the point’s coordinates...
Find the polar coordinates of a point with the Cartesian coordinates
 Find the polar coordinates of a point with the Cartesian coordinates (x,y)=(−4√2,4√2).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT