Hello, I am trying to create a program that will read the point-slope information for two lines and determine if they intersect or are parallel. Below is the code I have:
/*Point_Slope.ccp Reads the point and slope information for two lines and
* determines whether they intersect or are parallel. If
* they intersect, finds the point of intersection and
* determines if they are perpendicular to each other.
*
* Specification:
* Input(screen): point1 (x & y coordinates), slope1 (m1),
* point2 (x & y coordinates), slope2 (m2).
* Output(screen): intersect/parallel, if perpendicular.
*****************************************************************************/
#include <iostream> // cin, cout, >>, <<
#include "CartesianPoint.h"
#include "Line.h"
using namespace std;
int main()
{
CartesianPoint p1, p2;
double slope1, slope2;
cout << "Enter point and slope info. for line 1: ";
cin >> p1 >> slope1;
cout << "Enter point and slope info. for line 2: ";
cin >> p2 >> slope2;
Line line1(p1, slope1),
line2(p2, slope2);
cout << "\nline 1: ";
line1.PointSlope(cout);
cout << "\nline 2: ";
line2.PointSlope(cout);
if (slope1 == slope2)
cout << "Lines are parallel\n";
else
{
double xInt = (slope1 * p1.X() - slope2*p2.X() + p2.Y() - p1.Y())
/ (slope1 - slope2),
yInt = slope1 * (xInt - p1.X()) + p1.Y();
CartesianPoint pInt(xInt, yInt);
cout << "Lines intersect at " << pInt << " and they "
<< (slope1 * slope2 == -1 ? "are " : "are not ")
<< "perpendicular\n";
}
}
/* CartesianPoint.h provides the declaration of class CartesianPoint.
*******************************************************************/
#ifndef CARTESIANPOINT
#define CARTESIANPOINT
#include <iostream>
using namespace std;
class CartesianPoint
{
public:
CartesianPoint();
CartesianPoint(double xVal, double yVal);
double X() const;
double Y() const;
void SetX(double xVal);
void SetY(double yVal);
private:
double myX, myY;
};
inline CartesianPoint::CartesianPoint()
{ myX = 0; myY = 0;}
inline CartesianPoint::CartesianPoint(double xVal, double yVal)
{ myX = xVal; myY = yVal;}
inline double CartesianPoint::X() const { return myX;}
inline double CartesianPoint::Y() const { return myY;}
inline void CartesianPoint::SetX(double xVal) {myX = xVal;}
inline void CartesianPoint::SetY(double yVal) {myY = yVal;}
inline ostream & operator<<(ostream & out, CartesianPoint & point)
{
out << "(" << point.X() << ", " << point.Y() << ')';
return out;
}
inline istream & operator>>(istream & in, CartesianPoint & point)
{
char punc;
double x, y;
in >> punc >> x >> punc >> y >> punc;
point.SetX(x);
point.SetY(y);
return in;
}
#endif
/* LineSegment.h provides the declaration of class LineSegment.
*************************************************************/
#ifndef LINESEGMENT
#define LINESEGMENT
#include "CartesianPoint.h"
#include <iostream>
using namespace std;
class LineSegment
{
public:
double Slope();
void Print(ostream & out);
void Read(istream & in);
void FindPerpBisector();
private:
CartesianPoint first, last;
double slope;
};
inline double LineSegment::Slope()
{
if (first.X() == last.X())
{
cerr << "No slope -- line segment is vertical\n";
return 1E38;
}
return (last.Y() - first.Y()) / (last.X() - first.X());
}
inline void LineSegment::Read(istream & in)
{
in >> first >> last;
}
inline ostream & operator<<(ostream & out, LineSegment & seg)
{
seg.Print(out);
return out;
}
inline istream & operator>>(istream & in, LineSegment & seg)
{
seg.Read(in);
return in;
}
#endif
/* LineSegment.cpp defines the nontrivial members of class LineSegment.
*********************************************************************/
#include "LineSegment.h"
void LineSegment::Print(ostream & out)
{
if (first.X() == last.X())
cout << "x = " << first.X();
else
{
out << "y - " << first.Y() << " = "
<< Slope() << "(x - " << first.X() << ")";
}
}
void LineSegment::FindPerpBisector()
{
LineSegment perpBis;
CartesianPoint
midPt(first.X() + last.X())/2.0), (first.Y() + last.Y())/2.0);
perpBis.first = midPt;
perpBis.last.SetX(1 + midPt.X());
perpBis.last.SetY(-1.0 / Slope() + midPt.Y());
cout << perpBis;
}
/* Line.h provides the declaration of class Line.
***************************************************************/
#ifndef LINE
#define LINE
#include "CartesianPoint.h"
#include "LineSegment.h"
#include <iostream>
using namespace std;
class Line
{
public:
Line(CartesianPoint p, double m);
void PointSlope(ostream & out);
void SlopeIntercept(ostream & out);
private:
CartesianPoint point;
double slope;
};
inline Line::Line(CartesianPoint p, double m)
{
point.SetX(p.X());
point.SetY(p.Y());
slope = m;
}
inline void Line::PointSlope(ostream & out)
{
out << "y - " << point.Y() << " = "
<< slope << "(x - " << point.X() << ")" << endl;
}
inline void Line::SlopeIntercept(ostream & out)
{
out << "y = " << slope << "*x + "
<< -slope*point.X() + point.Y() << endl;
}
#endif
I am getting the error: error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'CartesianPoint' (or there is no acceptable conversion), along with another. But, I would like to ask for help with this one.
Any assistance would be greatful....thanks in advance.