Project Write a class called Point that contains two doubles that represent its
ID: 3698031 • Letter: P
Question
Project
Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and passes those values to the set methods to initialize its fields. It should have a default constructor that initializes both coordinates to 0 (also using the set methods). It should also contain a method called distanceTo that takes as a parameter a constant reference to another Point and returns the distance from the Point that was passed as a parameter to the Point that we called the method of. You will need to use sqrt(). For example at the end of the following, dist should be equal to 5.0:
Next, write a class called LineSegment that contains two Points that represent its two endpoints. It should have get and set methods for both fields and a constructor that takes two Point parameters and passes them to the set methods to initialize the data members. It should also contain a method called length that returns the length of the LineSegment – by using the distanceTo method on its endpoints – and a method called slope that returns the slope of the LineSegment. You don't need to do anything special for vertical lines - division by zero will result in the special value inf, which stands for "infinity". Your program will not be tested with line segments where both endpoints have the same coordinates. The LineSegement class might be used as follows:
The functions for the Point class should have the following names:
setXCoord, getXCoord
setYCoord, getYCoord
distanceTo
The functions for the LineSegment class should have the following names:
setEnd1, getEnd1
setEnd2, getEnd2
length
slope
The files must be named: Point.hpp, Point.cpp, LineSegment.hpp and LineSegment.cpp
Point.cpp and LineSegment.hpp should both #include Point.hpp. LineSegment.cpp should #include LineSegment.hpp. The main method you write for testing will also need to include LineSegment.hpp. If you named the file with your main method "geomMain.cpp", then you can compile your program with "g++ Point.cpp LineSegment.cpp geomMain.cpp -o geom".
Explanation / Answer
Any queries please comment
Point.hpp
#ifndef POINT_HPP
#define POINT_HPP
class Point
{
private:
double XCoord;
double YCoord;
public:
Point();
Point(double xVal, double yVal);
void setXCoord(double);
double getXCoord();
void setYCoord(double);
double getYCoord();
double distanceTo(const Point&);
};
point.cpp
#include <iostream>
#include <cmath>
#include "Point.hpp"
Point::Point()
{
setXCoord(0);
setYCoord(0);
}
Point::Point(double xVal, double yVal)
{
setXCoord(xVal);
setYCoord(yVal);
}
void Point::setXCoord(double xVal)
{
XCoord = xVal;
}
void Point::setYCoord(double yVal)
{
YCoord = yVal;
}
double Point::getXCoord()
{
return XCoord;
}
double Point::getYCoord()
{
return YCoord;
}
double Point::distanceTo(const Point &otherpoint)
{
double xVal1,
xVal2,
yVal1,
yVal2;
double distanceX,
distanceY,
distanceSq,
distance;
xVal1 = XCoord;
xVal2 = otherpoint.XCoord;
yVal1 = YCoord;
yVal2 = otherpoint.YCoord;
distanceX = xVal1 - xVal2;
distanceY = yVal1 - yVal2;
distanceSq = pow(distanceX, 2) + pow(distanceY, 2);
distance = sqrt(distanceSq);
return distance;
}
linesegment.hpp
#ifndef LINESEGMENT_HPP
#define LINESEGMENT_HPP
#include "Point.hpp"
class LineSegment
{
private:
Point p1;
Point p2;
public:
LineSegment();
LineSegment(Point, Point);
void setEnd1(Point);
double getEnd1();
void setEnd2(Point);
double getEnd2();
double length();
double slope();
};
Linesegment.cpp
#include <iostream>
#include "LineSegment.hpp"
LineSegment::LineSegment(Point p1, Point p2)
{
setEnd1(p1);
setEnd2(p2);
}
LineSegment::LineSegment()
{
setEnd1(point(0,0));
setEnd2(point(0,0));
}
void LineSegment::setEnd1(Point p1)
{
p1.setXCoord();
p1.setYCoord();
}
double LineSegment::getEnd1()
{
return p1;
}
void LineSegment::setEnd2(Point p2)
{
p2.setXCoord();
p2.setYCoord();
}
double LineSegment::getEnd2()
{
return p2;
}
double LineSegment::length()
{
return p1.distanceTo(p2);
}
double LineSegment::slope()
{
if (p2.getXCoord() - p1.getXCoord() == 0)
{
return -55555;
}
else
{
return(p2.getYCoord() - p1.getYCoord()) / (p2.getXCoord() - p1.getXCoord());
}
}
Geomain.cpp
#include <iostream>
#include "Point.hpp"
#include "LineSegment.hpp"
using namespace std;
int main()
{
Point p3, p4;
LineSegment linecheck1;
double userXCoord1 = 0;
double userYCoord1 = 0;
double userXCoord2 = 0;
double userYCoord2 = 0;
cout << "Please enter your X coordindate of Point 1: " << endl;
cin >> userXCoord1;
p3.setXCoord(userXCoord1);
cout << "Please enter your Y coordinate of Point 1: " << endl;
cin >> userYCoord1;
p3.setYCoord(userYCoord1);
cout << "Please enter your X coordinate of Point 2: " << endl;
cin >> userXCoord2;
p4.setXCoord(userXCoord2);
cout << "Please enter your Y coordinate of Point 2: " << endl;
cin >> userYCoord2;
p4.setYCoord(userYCoord2);
cout << "Point 1: " << p3.getXCoord() << "," << p3.getYCoord() << endl;
cout << "Point 2: " << p4.getXCoord() << "," << p4.getYCoord() << endl;
Point p1(p3.getXCoord(), p3.getYCoord());
Point p2(p4.getXCoord(), p4.getYCoord());
double dist = p1.distanceTo(p2);
cout << "The distance between Point 1 and Point 2 is: " << dist << endl;
cout << "The length between endpoints is: " << linecheck1.length() << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.