// CSE 303, Spring 2009, Marty Stepp // Implementation of a Point class, v2. #include #include "Point.h" // Constructs a new Point at the given x/y coordinates. // If no coordinates are specified, uses (0, 0). Point::Point(int x, int y) { setLocation(x, y); } // Returns the direct 2D distance between this point and the given point. // The method is set as 'const' to indicate that calling it doesn't modify // the Point's state. // The method takes a reference to a Point as a parameter so that the // client's parameter is not copied every time it is passed. double Point::distance(const Point& p) const { int dx = x - p.getX(); int dy = y - p.getY(); return sqrt(dx*dx + dy*dy); } // Returns the x coordinate of this Point. int Point::getX() const { return x; } // Returns the y coordinate of this Point. int Point::getY() const { return y; } // Sets the x/y coordinates of this Point to the given values. void Point::setLocation(int x, int y) { this->x = x; this->y = y; } // Shifts this point's location by the given amount. void Point::translate(int dx, int dy) { setLocation(x + dx, y + dy); } // This operator allows you to add two points. // The result is a new point whose x/y are the sum of the two x/y values. Point Point::operator+(const Point& p) const { Point result(x + p.getX(), y + p.getY()); return result; // return Point(this->x + p.getX(), this->y + p.getY()); } // This operator returns whether two points have the same x/y coordinates. bool Point::operator==(const Point& p) const { return x == p.getX() && y == p.getY(); } // This operator returns whether the given points have different x or y // coordinates. We need to write this because == does not give us != bool Point::operator!=(const Point& p) const { return !(*this == p); } // This operator allows us to print a point to an output stream. ostream& operator<<(ostream& out, const Point& p) { out << "(" << p.getX() << ", " << p.getY() << ")"; return out; }