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: 3676469 • 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

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

    // TODO: Fill me in

    point_t ret;

// ret.x_coord = 0.0;
ret.x_coord = a.x_coord;
// ret.y_coord = 0.0;
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 = 0.0;
ret.x_coord = b.x_coord;
//    ret.y_coord = 0.0;
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
  
    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(){

    // TODO: Fill me in
    //equation of line is y=mx+c where m is slope and c is Y intercept. Line is passing through a and b
    // So c=y-mx where x and y can be (a.x_coordinate,a.y_cordinate) or (b.x_cordinate,b.y_coordinate)
  
   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()
{

    // TODO: Fill me in
    // Distance foumula is d= squareroot((x2-x1)^2+(y2-y1)^2)
    double d=0.0;
    d= sqrt(pow((b.x_coord-a.x_coord),2)+pow((b.y_coord-a.y_coord),2));

    return d;

}
};