//****************************************************************// // // // Copyright (c) 1997. // // Dr. Michael A. Redmond // // // // This software may not be distributed further without permission from // // Dr Michael A. Redmond. // // // // This software is distributed WITHOUT ANY WARRANTY. No claims are made // // as to its functionality or purpose. // // // // Author: Dr Michael A. Redmond // // Date: 2/11/97 // // // // Overcommented because also explaining the language as well // as the program // //**************************************************************************// #include #if EXCEPT_HAND_OFF #endif #include // #include #include "my-math.h" #include "point.h" // default constructor // used if no parameters passed - uses default values - (0,0) Point::Point() { SetRow(0); SetCol(0); } // (row, col) constructor // used if 2 parameters passed Point::Point(int row, int col) { SetRow(row); SetCol(col); } // get the row number int Point::GetRow() const { return RowNo; } // get the column number int Point::GetCol() const { return ColNo; } // set the row number void Point::SetRow(int row) { RowNo = row; } // set the column number void Point::SetCol(int col) { ColNo = col; } // inspectors // give a position that is up by given amount and over by given amount // (down if negative) Point Point::Adjust(int horiz, int vert) const { return Point(GetRow() + horiz, GetCol() + vert); } // mutators - establish new values of attributes (data members) // facilitators // horizontal distance between two points int Point::HorizDist(Point secpoint) const { return (GetRow() - secpoint.GetRow()); } // vertical distance between two points int Point::VertDist(Point secpoint) const { return (GetCol() - secpoint.GetCol()); } // city block distance between two points int Point::CityBlockDist(Point secpoint) const { //return static_cast(fabs(HorizDist(secpoint)) + fabs(VertDist(secpoint))); return (myabs(HorizDist(secpoint)) + myabs(VertDist(secpoint))); } // crow flight distance between two points float Point::CrowDist(Point secpoint) const { int hdist, asquared; int vdist, bsquared; hdist = HorizDist(secpoint); asquared = hdist * hdist; vdist = VertDist(secpoint); bsquared = vdist * vdist; return sqrt(asquared + bsquared); } // inserting a Point // uses iostream operator << - insertion into a stream // should receive as an argument an iostream object // commonly cout - standard output void Point::Insert(ostream &sout) const { sout << "(" << GetRow() << "," << GetCol() << ")"; return; } // extracting a Point // uses iostream operator >> - extract from a stream into object(s) // should receive as an argument an iostream object // commonly cin - standard input void Point::Extract(istream &sin) { int row; int col; char open; char close; char comma; // obtain point location from user - pulling apart into // its components sin >> open >> row >> comma >> col >> close; // use mutator functions to update the data members // (the safe way - doesn't depend on implementation) SetRow(row); SetCol(col); return; } // inserting a Point // overloading built-in << so can use same syntax as with built-in object types ostream& operator<<(ostream &sout, const Point &pt) { // uses member function for the class to actually carry out // the operation // (this overloading is just syntactic sugar (but nice sugar)) // send a message to the rational object telling it to do an insert // into the stream object pt.Insert(sout); // return the result so that this insertion can be cascaded // with other insertions in a single expression return sout; } // extracting a Point // overloading built-in >> so can use same syntax as with built-in object types istream& operator>>(istream &sin, Point &pt) { // uses member function for the class to actually carry out // the operation // (this overloading is just syntactic sugar (but nice sugar)) // send a message to the rational object telling it to do an extraction // from the stream object into itself pt.Extract(sin); // return the stream object so that this extraction can be cascaded // with other extractions in a single expression return sin; } bool Point::PointsEqual(const Point &right) const { return ((GetRow() == right.GetRow()) && (GetCol() == right.GetCol())); } bool operator==(const Point &left, const Point &right) { return left.PointsEqual(right); }