Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create (fill-in) the following functions. Some member functions have already bee

ID: 3676914 • Letter: C

Question

Create (fill-in) the following functions. Some member functions have already been created. There were six functions before the function 'isHorizontal()', and I already finished them. So I only need someone can help me to create (fill-in) the functions which are from 7 to 11.

1. Line(a, b).- This is a constructor with initial data with two given structs.

2. getFirstPoint() - This member function returns the first point given.   

3. getSecondPoint() - This member function returns the second point given.

4. getSlope() - This member function returns the slope of a line defined by the two points. This should return if it is vertical.

5. getIntercept() - This member function returns the intercept of a line, or 0 if it is vertical.

6. getLength() - This member function returns the distance between the two points given to the line.

7. isHorizontal() - This member function returns whether a line is horizontal (i.e., in parallel to the x-axis).

8. isVertical() - This member function returns whether a line is vertical (i.e., in parallel to the y-axis).

9. getDistance(line2) - This member function computes and returns the distance between the current and Line2, specified as a parameter, if they are parallel. If not parallel, return 0, since they intersect.

10. isParallel(line2) - This member function checks whether the current line and line2 are parallel.

11. isPerpendicular(line2) - This member function checks whether the current line and line2 are perpendicular.

// Source File: lines.cpp

// Description: Implements a line class. Provide two points on the

// line and this class can tell you the slope, intercept, and if two

// lines are paralell, perpendicular, and their intersection point.

#include <iostream>

#include <sstream>

#include <limits>

#include <cmath>

using namespace std;

const double DDIFF = 0.0001;

const double inf = std::numeric_limits<double>::infinity();

struct point_t{

double x_coord, y_coord;

};

class Line{

private:

point_t a, b;

  

public:

  

// Default constructor makes 2 points at 0,0

Line(){

point_t zero;

zero.x_coord = 0.0;

zero.y_coord = 0.0;

setPoints(zero, zero);

}

  

// Take two points to define a line.

Line(point_t a_temp, point_t b_temp){

a = a_temp;

b = b_temp;

}

// Assign the given points to this line

void setPoints(point_t a_temp, point_t b_temp){

a = a_temp;

b = b_temp;

}

  

// Return the first point of the line

point_t getFirstPoint(){

point_t ret;

ret.x_coord = a.x_coord;

ret.y_coord = a.y_coord;

return ret;

}

  

// Return the second point of the line

point_t getSecondPoint(){

point_t ret;

ret.x_coord = b.x_coord;

ret.y_coord = b.y_coord;

return ret;

}

  

// Given the line, what is the slope? Return inf (constant above) if

// the the line is vertical (no x difference).

double getSlope(){

double slope = 0.0;

if(b.x_coord - a.x_coord == 0)

return inf;

else

slope = (b.y_coord - a.y_coord) / (b.x_coord - a.x_coord);

return slope;

}

  

// Returns the intercept (the y location where the line intercepts the y axis).

// If the line is vertical, return 0;

double getIntercept(){

double slope = getSlope();

double intercept = 0.0;

if(slope == inf)

return 0.0;

else

intercept = a.y_coord - slope * a.x_coord;

return intercept;

}

  

// Returns a string containing point1 point2

string getLineInfo(){

stringstream temp;

temp << a.x_coord << "," << a.y_coord << " " << b.x_coord << "," << b.y_coord;

return temp.str();

}

// Returns the length of the line between the two points

double getLength(){

double d = 0.0;

d = sqrt(pow((b.x_coord-a.x_coord),2)+pow((b.y_coord-a.y_coord),2));

return d;

}

  

// Returns true if the line is horizontal

bool isHorizontal(){

// TODO: Fill me in

return true;

}

  

// Returns true if the line is vertical

bool isVertical(){

// TODO: Fill me in

return true;

}

// Return the distance between two lines. If they intersect, return 0.0.

// Return value should always be positive.

double getDistance(Line line2){

// TODO: Fill me in

return 0.0;

}

// Return true if the two lines are parallel. Be sure to return the

// correct result if they are both vertical!

bool isParallel(Line line2){

// TODO: Fill me in

return true;

  

}

  

// Return true if the two lines are perpendicular. Be sure to return the

// correct result if they one is vertical.

bool isPerpendicular(Line line2){

// TODO: Fill me in

return true;

}

};

Explanation / Answer

Please find the completed code below :

#include <iostream>
#include <sstream>
#include <limits>
#include <cmath>
using namespace std;

const double DDIFF = 0.0001;
const double inf = std::numeric_limits<double>::infinity();

struct point_t{
double x_coord, y_coord;
};

class Line{
private:
point_t a, b;
  
public:
  
// Default constructor makes 2 points at 0,0
Line(){
point_t zero;
zero.x_coord = 0.0;
zero.y_coord = 0.0;
setPoints(zero, zero);
}
  
// Take two points to define a line.
Line(point_t a_temp, point_t b_temp){
a = a_temp;
b = b_temp;
}

// Assign the given points to this line
void setPoints(point_t a_temp, point_t b_temp){
a = a_temp;
b = b_temp;
}
  
// Return the first point of the line
point_t getFirstPoint(){
point_t ret;
ret.x_coord = a.x_coord;
ret.y_coord = a.y_coord;
return ret;
}
  
// Return the second point of the line
point_t getSecondPoint(){
point_t ret;
ret.x_coord = b.x_coord;
ret.y_coord = b.y_coord;
return ret;
}
  
// Given the line, what is the slope? Return inf (constant above) if
// the the line is vertical (no x difference).
double getSlope(){
double slope = 0.0;
if(b.x_coord - a.x_coord == 0)
return inf;
else
slope = (b.y_coord - a.y_coord) / (b.x_coord - a.x_coord);
return slope;
}
  
// Returns the intercept (the y location where the line intercepts the y axis).
// If the line is vertical, return 0;
double getIntercept(){
double slope = getSlope();
double intercept = 0.0;
if(slope == inf)
return 0.0;
else
intercept = a.y_coord - slope * a.x_coord;
return intercept;
}
  
// Returns a string containing point1 point2
string getLineInfo(){
stringstream temp;
temp << a.x_coord << "," << a.y_coord << " " << b.x_coord << "," << b.y_coord;
return temp.str();
}

// Returns the length of the line between the two points
double getLength(){
double d = 0.0;
d = sqrt(pow((b.x_coord-a.x_coord),2)+pow((b.y_coord-a.y_coord),2));
return d;
}
  
// Returns true if the line is horizontal
bool isHorizontal(){
  
   point_t a,b;
   a = getFirstPoint();
   b = getSecondPoint();
  
   if(a.x_coord == b.x_coord)
       return true;
   else
       return false;
}
  
// Returns true if the line is vertical
bool isVertical(){
       point_t a,b;
       a = getFirstPoint();
       b = getSecondPoint();
      
       if(a.y_coord == b.y_coord)
           return true;
       else
           return false;
}

// Return the distance between two lines. If they intersect, return 0.0.
// Return value should always be positive.
double getDistance(Line line2){
   double d = 0.0;
   d = sqrt(pow((b.x_coord-a.x_coord),2)+pow((b.y_coord-a.y_coord),2));
   return d;
}

// Return true if the two lines are parallel. Be sure to return the
// correct result if they are both vertical!
bool isParallel(Line line2){
       point_t a,b;
       a = getFirstPoint();
       b = getSecondPoint();
      
       c = line2.a;
       d = line2.b;
      
       double m1 = (b.y_coord - a.y_coord)/(b.x_coord - b.x_coord);
       double m2 = (d.y_coord - c.y_coord)/(d.x_coord - c.x_coord);
       if(m1==m2)
           return true;
       else
           return false;
}
  
// Return true if the two lines are perpendicular. Be sure to return the
// correct result if they one is vertical.
bool isPerpendicular(Line line2){
       point_t a,b;
       a = getFirstPoint();
       b = getSecondPoint();
      
       c = line2.a;
       d = line2.b;
      
       double m1 = (b.y_coord - a.y_coord)/(b.x_coord - b.x_coord);
       double m2 = (d.y_coord - c.y_coord)/(d.x_coord - c.x_coord);
       if(m1*m2==-1)
           return true;
       else
           return false;
}
};