Write a class called Point that contains two doubles that represent its x- and y
ID: 3760190 • Letter: W
Question
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. 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: Point p1(-1.5, 0.0); Point p2(1.5, 4.0); double dist = p1.distanceTo(p2);
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 (if the LineSegment is vertical, go ahead and return the value you get when dividing doubles by zero, which is infinity).
The LineSegement class might be used as follows: Point p1(4.3, 7.52); Point p2(-17.0, 1.5); LineSegment ls1(p1, p2); double length = ls1.length(); double slope = ls1.slope();
Do not include a main method in the files you submit - just the definition of your Point and LineSegment classes. I will be including a main method for testing, and there can only be one main method in a program. You will of course need to have a main method for testing purposes - just make sure you delete it or comment it out before submitting your files. 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".
So I saw the answer to this question posted and was wondering if someone could explain a couple of lines of code in the LineSegment.cpp, this is my version of the code(not direct from a previous answer). The two if/else statements I am having trouble understanding. I know it says to return infinity if dividing a double by zero. Guess I am not sure why for the first if it has a return of -55555 and I dont understand the whole bool and isinf line. So if anyone can explain the two if/else methods at the end I would appreciate it.
#include "LineSegment.hpp"
#include <iostream>
using namespace std;
LineSegment::LineSegment ()
{
Point pnt1;
Point pnt2;
setEnd1 (pnt1);
setEnd2 (pnt2);
}
LineSegment::LineSegment (Point &pnt1, Point &pnt2) //constructor with parameters
{
setEnd1 (pnt1);
setEnd2 (pnt2);
}
void LineSegment::setEnd1 (Point &pnt) //method to set endpoint 1
{
pnt1.setXCoord(pnt.getXCoord());
pnt1.setYCoord(pnt.getYCoord());
}
void LineSegment::setEnd2 (Point &pnt) //method to set endpoint 2
{
pnt2.setXCoord(pnt.getXCoord());
pnt2.setYCoord(pnt.getYCoord());
}
Point &LineSegment::getEnd1 () //method to get endpoint 1
{
return pnt1;
}
Point &LineSegment::getEnd2 () //method to get endpoint 2
{
return pnt2;
}
double LineSegment::length() //method to get length of line, uses function from Point
{
return pnt1.distanceTo(pnt2);
}
double LineSegment::slope() //calculate the slope of the line
{
if (pnt2.getXCoord() - pnt1.getXCoord() == 0)
{
return -55555;
}
else
{
return (pnt2.getYCoord() - pnt1.getYCoord()) /
(pnt2.getXCoord() - pnt1.getXCoord());
}
}
bool LineSegment::isinf() {
if (pnt2.getXCoord() - pnt1.getXCoord() ==0)
{
return true;
}
else
{
return false; }
}
Explanation / Answer
Answer :
In general slope of line =(y2-y1)/(x2-x1)
double LineSegment::slope() //calculate the slope of the line
{
if (pnt2.getXCoord() - pnt1.getXCoord() == 0) //Condtion to check weather denominator value i.e x2-x1 is equal to 0 or not
{
return -55555; //If the value of x2-x1 is 0 then in this case of the fraction representing the slope of a line, the fraction is undefined. So since it is undefined user is returning some garbage type value as -55555
}
else
{
return (pnt2.getYCoord() - pnt1.getYCoord()) /
(pnt2.getXCoord() - pnt1.getXCoord()); //If the denominator value is defined then this line of code returns the value of the slope of the line for points x1,y1 and x2 y2.
}
}
bool LineSegment::isinf() { //This function is defined to test or check whether denominator value is defined or undefined i.e in other words finite or infinite
if (pnt2.getXCoord() - pnt1.getXCoord() ==0) //Condtion to check weather denominator value i.e x2-x1 is equal to 0 or not if it is equal to 0 then it means infinite or undefined else finite or defined
{
return true; //Return value true if the value x2-x1==0 i.e is infinite or undefined to function
}
else
{
return false; //Return value false if the value x2-x1!=0 i.e is finite or defined to function
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.