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: 3676468 • Letter: C

Question

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

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.

#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){

// TODO: Fill me in

}

// 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(){

// TODO: Fill me in

point_t ret;

ret.x_coord = 0.0;

ret.y_coord = 0.0;

return ret;

}

  

// Return the second point of the line

point_t getSecondPoint(){

// TODO: Fill me in

point_t ret;

ret.x_coord = 0.0;

ret.y_coord = 0.0;

return ret;

}

  

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

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

double getSlope(){

// TODO: Fill me in

return 0.0;

}

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

// If the line is vertical, return 0;

double getIntercept(){

// TODO: Fill me in

return 0.0;

}

// 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(){

// TODO: Fill me in

return 0.0;

}

Thank you very much!

Explanation / Answer

Here is the filled in logic for you:

#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){
// TODO: Fill me in
/*point_t first;
point_t second;
first = a_temp;
second = b_temp;
first.x_coord = a_temp.x_coord;
first.y_coord = a_temp.y_coord;
second.x_coord = b_temp.x_coord;
second.y_coord = b_temp.y_coord;*/
setPoints(a_temp, 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(){
// TODO: Fill me in
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(){
// TODO: Fill me in
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(){
// TODO: Fill me in
if(a.x_coord - b.x_coord == 0)
return inf;
return (a.y_coord - b.y_coord) / (a.x_coord - b.x_coord);
}
// Returns the intercept (the y location where the line intercepts the y axis).
// If the line is vertical, return 0;
double getIntercept(){
// TODO: Fill me in
return getSlope() * (0 - a.x_coord) + a.y_coord;
}
// 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(){
// TODO: Fill me in
return sqrt(pow((b.x_coord - a.x_coord),2) + pow((b.y_coord - a.y_coord), 2));
}
};

If you need any refinements, just get back to me.