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

Can someone help me to create (fill-in) the following functions. Some member fun

ID: 3676852 • Letter: C

Question

Can someone help me to create (fill-in) the following functions. Some member functions have already been created.

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

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

3. 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.

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

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

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote