From bf160f210a5cb3a05add2684b915379b64cdcfa1 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Sun, 10 Nov 2002 11:25:40 +0000 Subject: [PATCH] make methods for Rect that use Point --- otk/rect.cc | 67 +++++++++++++++++++++++++++++++++++++++++++---------- otk/rect.hh | 38 +++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 13 deletions(-) diff --git a/otk/rect.cc b/otk/rect.cc index db53b37e..f5068efe 100644 --- a/otk/rect.cc +++ b/otk/rect.cc @@ -2,7 +2,8 @@ namespace otk { -void Rect::setX(int x) { +void Rect::setX(int x) +{ _x2 += x - _x1; _x1 = x; } @@ -15,7 +16,17 @@ void Rect::setY(int y) } -void Rect::setPos(int x, int y) { +void Rect::setPos(const Point &location) +{ + _x2 += location.x() - _x1; + _x1 = location.x(); + _y2 += location.y() - _y1; + _y1 = loaction.y(); +} + + +void Rect::setPos(int x, int y) +{ _x2 += x - _x1; _x1 = x; _y2 += y - _y1; @@ -23,28 +34,46 @@ void Rect::setPos(int x, int y) { } -void Rect::setWidth(unsigned int w) { +void Rect::setWidth(unsigned int w) +{ _x2 = w + _x1 - 1; } -void Rect::setHeight(unsigned int h) { +void Rect::setHeight(unsigned int h) +{ _y2 = h + _y1 - 1; } -void Rect::setSize(unsigned int w, unsigned int h) { +void Rect::setSize(unsigned int w, unsigned int h) +{ _x2 = w + _x1 - 1; _y2 = h + _y1 - 1; } -void Rect::setRect(int x, int y, unsigned int w, unsigned int h) { +void Rect::setSize(const Point &size) +{ + _x2 = size.x() + _x1 - 1; + _y2 = size.y() + _y1 - 1; +} + + +void Rect::setRect(int x, int y, unsigned int w, unsigned int h) +{ *this = Rect(x, y, w, h); } -void Rect::setCoords(int l, int t, int r, int b) { +void setRect(const Point &location, const Point &size) +{ + *this = Rect(location, size); +} + + +void Rect::setCoords(int l, int t, int r, int b) +{ _x1 = l; _y1 = t; _x2 = r; @@ -52,7 +81,17 @@ void Rect::setCoords(int l, int t, int r, int b) { } -Rect Rect::operator|(const Rect &a) const { +void setCoords(const Point &tl, const Point &br) +{ + _x1 = tl.x(); + _y1 = tl.y(); + _x2 = br.x(); + _y2 = br.y(); +} + + +Rect Rect::operator|(const Rect &a) const +{ Rect b; b._x1 = std::min(_x1, a._x1); @@ -64,7 +103,8 @@ Rect Rect::operator|(const Rect &a) const { } -Rect Rect::operator&(const Rect &a) const { +Rect Rect::operator&(const Rect &a) const +{ Rect b; b._x1 = std::max(_x1, a._x1); @@ -76,19 +116,22 @@ Rect Rect::operator&(const Rect &a) const { } -bool Rect::intersects(const Rect &a) const { +bool Rect::intersects(const Rect &a) const +{ return std::max(_x1, a._x1) <= std::min(_x2, a._x2) && std::max(_y1, a._y1) <= std::min(_y2, a._y2); } -bool Rect::contains(int x, int y) const { +bool Rect::contains(int x, int y) const +{ return x >= _x1 && x <= _x2 && y >= _y1 && y <= _y2; } -bool Rect::contains(const Rect& a) const { +bool Rect::contains(const Rect& a) const +{ return a._x1 >= _x1 && a._x2 <= _x2 && a._y1 >= _y1 && a._y2 <= _y2; } diff --git a/otk/rect.hh b/otk/rect.hh index 925456a1..dc3d05d6 100644 --- a/otk/rect.hh +++ b/otk/rect.hh @@ -26,6 +26,14 @@ public: */ inline Rect(int x, int y, unsigned int w, unsigned int h) : _x1(x), _y1(y), _x2(w + x - 1), _y2(h + y - 1) { } + //! Constructs a Rect from 2 Point objects + /*! + @param location The point defining the top left corner of the rectangle + @param size The width and height of the rectangle + */ + inline Rect(const Point &location, const Point &size) + : _x1(location.x()), _y1(location.y()), + _x2(size.x() + location.x() - 1), _y2(size.y() + location.y() - 1) { } //! Constructs a Rect from an XRectangle inline explicit Rect(const XRectangle& xrect) : _x1(xrect.x), _y1(xrect.y), _x2(xrect.width + xrect.x - 1), @@ -44,6 +52,9 @@ public: inline int x(void) const { return _x1; } //! The y component of the point defining the top left corner of the Rect inline int y(void) const { return _y1; } + //! Returns the Point that defines the top left corner of the rectangle + inline Point location() const { return Point(_x1, _y1); } + //! Sets the x coordinate of the Rect. /*! @param x The new x component of the point defining the top left corner of @@ -64,11 +75,19 @@ public: the rectangle */ void setPos(int x, int y); + //! Sets the x and y coordinates of the Rect. + /*! + @param location The point defining the top left corner of the rectangle. + */ + void setPos(const Point &location); //! The width of the Rect inline unsigned int width(void) const { return _x2 - _x1 + 1; } //! The height of the Rect inline unsigned int height(void) const { return _y2 - _y1 + 1; } + //! Returns the size of the Rect + inline Point size() const { return Point(_x2 - _x1 + 1, _y2 - _y1 + 1); } + //! Sets the width of the Rect /*! @param w The new width of the rectangle @@ -79,12 +98,17 @@ public: @param h The new height of the rectangle */ void setHeight(unsigned int h); - //! Sets the width of the Rect. + //! Sets the size of the Rect. /*! @param w The new width of the rectangle @param h The new height of the rectangle */ void setSize(unsigned int w, unsigned int h); + //! Sets the size of the Rect. + /*! + @param size The new size of the rectangle + */ + void setSize(const Point &size); //! Sets the position and size of the Rect /*! @@ -96,6 +120,12 @@ public: @param h The new height of the rectangle */ void setRect(int x, int y, unsigned int w, unsigned int h); + //! Sets the position and size of the Rect + /*! + @param location The new point defining the top left corner of the rectangle + @param size The new size of the rectangle + */ + void setRect(const Point &location, const Point &size); //! Sets the position of all 4 sides of the Rect /*! @@ -105,6 +135,12 @@ public: @param b The new bottom coordinate of the rectangle */ void setCoords(int l, int t, int r, int b); + //! Sets the position of all 4 sides of the Rect + /*! + @param tl The new point at the top left of the rectangle + @param br The new point at the bottom right of the rectangle + */ + void setCoords(const Point &tl, const Point &br); //! Determines if two Rect objects are equal /*! -- 2.45.2