Implement a Line class that uses two points which represent
Solution
Here is the code for Line.h:
#include <iostream>
using namespace std;
struct Point
{
double x;
double y;
Point(double a = 0, double b = 0)
{
x = a;
y = b;
}
void display(ostream& out)
{
out << \"(\" << x << \", \" << y << \")\";
}
};
class Line
{
Point p1, p2;
public:
Line();
Line(Point a, Point b);
void setFirstPoint(Point a);
void setSecondPoint(Point a);
Point getFirstPoint() const;
Point getSecondPoint() const;
bool slope(double& m) const;
bool yIntercept(double& b) const;
bool isParallel(Line l) const;
bool isCollinear(Line l) const;
bool isPerpendicular(Line l) const;
Point intersect(Line l) const;
void display(ostream& out) const;
};
And the code for Line.cpp is:
#include \"Line.h\"
Line::Line()
{
p1 = Point(0, 0);
p2 = Point(1, 1);
}
Line::Line(Point a, Point b)
{
p1 = Point(a.x, a.y);
p2 = Point(b.x, b.y);
}
void Line::setFirstPoint(Point a)
{
p1 = a;
}
void Line::setSecondPoint(Point a)
{
p2 = a;
}
Point Line::getFirstPoint() const
{
return p1;
}
Point Line::getSecondPoint() const
{
return p2;
}
bool Line::slope(double& m) const
{
if(p1.x - p2.x == 0)
return false;
m = (p1.y - p2.y) / (double)(p1.x - p2.x);
return true;
}
bool Line::yIntercept(double& b) const
{
double m;
if(this->slope(m))
{
b = p1.y - m * p1.x;
return true;
}
return false;
}
bool Line::isParallel(Line l) const
{
double m1, m2;
if(this->slope(m1) && l.slope(m2))
if(m1 == m2)
return true;
return false;
}
bool Line::isCollinear(Line l) const
{
double m1, m2, b1, b2;
if(this->slope(m1) && l.slope(m2))
{
this->yIntercept(b1);
l.yIntercept(b2);
if(m1 == m2 && b1 == b2)
return true;
}
return false;
}
bool Line::isPerpendicular(Line l) const
{
double m1, m2;
if(this->slope(m1) && l.slope(m2))
{
if(-1/m1 == m2)
return true;
}
else if(!this->slope(m1) && m2 == 0)
return true;
else if(m1 == 0 && !this->slope(m2))
return true;
return false;
}
Point Line::intersect(Line l) const
{
return p1;
}
void Line::display(ostream& out) const
{
double m, b;
this->slope(m);
this->yIntercept(b);
out << \"y = \" << m << \"x + \" << b;
}


