Can someone help me to create (fill-in) the following functions. Some member fun
ID: 3676853 • 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
#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:
Line(){
point_t zero;
zero.x_coord = 0.0;
zero.y_coord = 0.0;
setPoints(zero, zero);
}
Line(point_t a_temp, point_t b_temp){
a = a_temp;
b = b_temp;
}
void setPoints(point_t a_temp, point_t b_temp){
a = a_temp;
b = b_temp;
}
point_t getFirstPoint(){
point_t ret;
ret.x_coord = a.x_coord;
ret.y_coord = a.y_coord;
return ret;
}
point_t getSecondPoint(){
point_t ret;
ret.x_coord = b.x_coord;
ret.y_coord = b.y_coord;
return ret;
}
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;
}
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;
}
string getLineInfo(){
stringstream temp;
temp << a.x_coord << "," << a.y_coord << " " << b.x_coord << "," << b.y_coord;
return temp.str();
}
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;
}
bool isHorizontal(){
double slope = getSlope();
if (slope == 0.0)
return true;
return false;
}
bool isVertical(){
double slope = getSlope();
if (slope == inf)
return true;
return false;
}
double getDistance(Line line2){
if (isParallel(line2) == false)
return 0.0;
return abs(-1*line2.getSlope()*line2.a.x_coord + line2.a.y_coord - (-1*getSlope()*a.x_coord + a.y_coord))/sqrt(getSlope()*line2.getSlope() + 1);
}
bool isParallel(Line line2){
double slope_1 = getSlope();
double slope_2 = line2.getSlope();
if (slope_1 == slope_2)
return true;
return false;
}
bool isPerpendiulcar(Line line2){
double slope_1 = getSlope();
double slope_2 = line2.getSlope();
if (slope_1*slope_2 == -1)
return true;
return false;
}
};
int main(){
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.