// Point.h creates a Point class and a related set of operations #include #include #include // Global constants ///////////////////////////////////////////////////// const double MAX_SLOPE = 99999.999; const double MAX_SLOPE_NEG = -99999.999; // Point class header section /////////////////////////////////////////// class Point // Friend functions { // Returns the distance // between two points friend double Distance ( const Point& p1, const Point& p2 ); // Returns the slope of // a line between two // points as a pseudo-point friend Point Slope ( const Point& p1, const Point& p2 ); // Returns the slope // as a double. friend double SlopeDouble( const Point& p1, const Point& P2 ); // Input a point friend istream& operator >> ( istream& is, Point& p ); // Output a point friend ostream& operator << ( ostream& os, const Point& p ); public: // Public member functions // Defaults to origin Point( double x_val = 0.0, double y_val = 0.0 ); Point( const Point& p ); // Copy constructor ~Point(); // Destructor // Accesses the x coor double Xcoor() const ; // Accesses the y coor double Ycoor() const ; // Returns the distance // from the origin to // the point double RayDistance() const; // Returns the quadrant // 1, 2, 3, or 4 int Quadrant() const ; // I was taught Q1: x>=0,y>=0; Q2: x<0, y>=0 // Q3: x<0, y<0; Q4: x>=0,y<0 static int GetCount(); // returns # Points instantiated private: double x; // X and Y cartesian coordinates double y; static int numPoints; // Keeps total # of Points }; ////////////////////////////////////////////////////////////////////////// // Point class implementation section //////////////////////////////////// int Point:: numPoints = 0; // initialize # of points. Note: no "static" //////////////////////////////////////////////////////////////// // Returns the distance between two points double Distance ( const Point& p1, const Point& p2 ) { return sqrt( (p2.x-p1.x)*(p2.x-p1.x) + (p2.y-p1.y)*(p2.y-p1.y) ); } //////////////////////////////////////////////////////////////// // Returns the slope of a line between two // points as a pseudo-point Point Slope ( const Point& p1, const Point& p2 ) { Point s; s.y = p2.y-p1.y; s.x = p2.x-p1.x; return s; } //////////////////////////////////////////////////////////////// // Returns the slope as a double double SlopeDouble( const Point& p1, const Point& p2 ) { double value; Point s = Slope( p1, p2 ); if ( s.x == 0.0 ) return MAX_SLOPE; else { value = s.y / s.x; if( value > MAX_SLOPE ) return MAX_SLOPE; else if( value < MAX_SLOPE_NEG ) return MAX_SLOPE_NEG; else return value; } } //////////////////////////////////////////////////////////////// // friend function that inputs a Point istream& operator >> ( istream& is, Point& p ) { is >> p.x >> p.y; return is; } //////////////////////////////////////////////////////////////// // friend function that outputs a Point ostream& operator << ( ostream& os, const Point& p ) { os << "( " << p.x << ", " << p.y << " )"; return os; } //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// // Constructor function. Defaults to origin Point :: Point( double x_val, double y_val ) { x = x_val; y = y_val; Point::numPoints++; // "Point::" is not required here //cout << "in const numPoints becoming " << numPoints << endl; } //////////////////////////////////////////////////////////////// // Copy constructor Point :: Point( const Point& p ) { x = p.x; y = p.y; Point::numPoints++; // "Point::" is not required here //cout << "in copy const numPoints becoming " << numPoints << endl; } //////////////////////////////////////////////////////////////// // Destructor Point:: ~Point() { x = 0; y = 0; numPoints--; //cout << "in dest numPoints becoming " << numPoints << endl; } //////////////////////////////////////////////////////////////// // Extracts the x coor double Point :: Xcoor() const { return x; } //////////////////////////////////////////////////////////////// // Extracts the y coor double Point :: Ycoor() const { return y; } //////////////////////////////////////////////////////////////// // Returns the distance from the origin to the point double Point :: RayDistance() const { Point origin; return Distance(*this, origin); // return the distance between the current point and the origin. } //////////////////////////////////////////////////////////////// // Returns the quadrant 1, 2, 3, or 4 int Point :: Quadrant() const { if ( x >= 0.0 ) if ( y >= 0.0 ) return 1; else return 4; else if ( y >= 0.0 ) return 2; else return 3; } ///////////////////////////////////////////////////////////////////////// int Point:: GetCount() // note "static" does not reappear here { return Point::numPoints; // "Point::" is not required } /////////////////////////////////////////////////////////////////////////