I need functions which are from \'isHorizontal()\' to \'isPerpendicular(line2)\'
ID: 3676868 • Letter: I
Question
I need functions which are from 'isHorizontal()' to 'isPerpendicular(line2)'. There were six functions before 'isHorizontal()', and I already finished them. So I only need someone can help me to create (fill-in) the rest functions.
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
// Returns true if the line is horizontal
bool isHorizontal(){
slope = getSlope();
//slope of horizontal line is zero.
if(slope==0)
return true;
}
// Returns true if the line is vertical
bool isVertical(){
slope = getSlope();
//slope of vertical line is infinity.
if(slope==inf)
return true;
}
// Return the distance between two lines. If they intersect, return 0.0.
// Return value should always be positive.
double getDistance(Line line2){
//if slopes of two lines are equal, then they are parallel and never intersect
//if slopes of two lines are not equal, they intersect somewhere
double slope1,slope2;
//get slope of line1
slope1 = getSlope();
//get slope of line2
slope2 = getSlope();
If(slope1!=slope2)
return 0.0;
else {
double d = 0.0;
//distance between two points a1(x,y) and a2(x,y) of the two lines.
d = sqrt(pow((a2.x_coord-a1.x_coord),2)+pow((a2.y_coord-a1.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){
//slopes of parallel lines are equal
double slope1,slope2;
//get slope of line1
slope1 = getSlope();
//get slope of line2
slope2 = getSlope();
If(slope1==slope2)
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){
// product of slopes of perpendicular lines is -1(negative reciprocal)
double slope1,slope2;
//get slope of line1
slope1 = getSlope();
//get slope of line2
slope2 = getSlope();
If((slope1*slope2)==-1)
return true;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.