home *** CD-ROM | disk | FTP | other *** search
- #ifndef _MISC_HPP
- #define _MISC_HPP
-
- // ------------------------------------------------------------------
- // File: MISC.HPP
- // Path: ...\REHACK\GENERAL\MISC.HPP
- // Version: 0.01
- // Author: Pat Reilly
- // CIS Id: 71333,2764
- // Created On: 6/18/93
- // Modified On: 6/27/93
- // Description: Miscellaneous REHACK classes and definitions; Point,
- // Rect. See MISC.TXT for more details.
- // Tabs: 4
- // ------------------------------------------------------------------
-
- #ifndef _TYPES_HPP
- #include "..\GENERAL\TYPES.HPP"
- #endif
-
- // Class Point
-
- struct Point
- {
- int x,y;
-
- Point& operator += (const Point& v);
- Point& operator -= (const Point& v);
- Point operator - () const;
- Point operator + () const;
- };
-
- // In-line definitions.
-
- inline Point& Point::operator += (const Point& p)
- {
- x += p.x;
- y += p.y;
- return *this;
- }
-
- inline Point& Point::operator -= (const Point& p)
- {
- x -= p.x;
- y -= p.y;
- return *this;
- }
-
- inline Point Point::operator - () const
- {
- Point t;
- t.x = -x;
- t.y = -y;
- return t;
- }
-
- inline Point Point::operator + () const
- {
- Point t;
- t = *this;
- return t;
- }
-
- // Related operators.
-
- inline bool operator == (const Point& p1, const Point& p2)
- {
- return bool(p1.x == p2.x && p1.y == p2.y);
- }
-
- inline bool operator != (const Point& p1, const Point& p2)
- {
- return bool(p1.x != p2.x || p1.y != p2.y);
- }
-
- inline Point operator + (const Point& point, const Point& p)
- {
- Point pt = point;
- pt += p;
- return pt;
- }
-
- inline Point operator - (const Point& point, const Point& p)
- {
- Point pt = point;
- pt -= p;
- return pt;
- }
-
- // Class Rect
-
- struct Rect
- {
- Point topLeft,bottomRight;
-
- Rect();
- Rect(int, int, int, int);
- Rect(const Point&, const Point&);
- Rect(const Rect&);
-
- bool isEmpty() const;
- bool contains(const Point&) const;
- void intersectWith(const Rect&);
- void unionWith(const Rect&);
- void move(const Point&);
- int width() const;
- int height() const;
-
- bool operator == (const Rect&) const;
- bool operator != (const Rect&) const;
- Rect& operator += (const Point&);
- Rect& operator -= (const Point&);
- Rect& operator |= (const Rect&);
- Rect& operator &= (const Rect&);
- };
-
- inline Rect::Rect()
- {}
-
- inline Rect::Rect(int left, int top, int right, int bottom)
- {
- topLeft.x = left;
- topLeft.y = top;
- bottomRight.x = right;
- bottomRight.y = bottom;
- }
-
- inline Rect::Rect(const Point& tl, const Point& br)
- {
- topLeft = tl;
- bottomRight = br;
- }
-
- inline Rect::Rect(const Rect& rect)
- {
- topLeft = rect.topLeft;
- bottomRight = rect.bottomRight;
- }
-
- inline bool Rect::isEmpty() const
- {
- return bool(bottomRight.x <= topLeft.x || bottomRight.y <= topLeft.y);
- }
-
- inline bool Rect::contains(const Point& point) const
- {
- return bool(point.x >= topLeft.x && point.x < bottomRight.x &&
- point.y >= topLeft.y && point.y < bottomRight.y);
- }
-
- inline void Rect::intersectWith(const Rect& rect)
- {
- if(rect.topLeft.x > topLeft.x)
- topLeft.x = rect.topLeft.x;
- if(rect.bottomRight.x < bottomRight.x)
- bottomRight.x = rect.bottomRight.x;
- if(rect.topLeft.y > topLeft.y)
- topLeft.y = rect.topLeft.y;
- if(rect.bottomRight.y < bottomRight.y)
- bottomRight.y = rect.bottomRight.y;
- }
-
- inline void Rect::unionWith(const Rect& rect)
- {
- // Handle special case of *this or rect being empty
- if(isEmpty())
- *this = rect;
- else if(!rect.isEmpty())
- {
- if(rect.topLeft.x < topLeft.x)
- topLeft.x = rect.topLeft.x;
- if(rect.bottomRight.x > bottomRight.x)
- bottomRight.x = rect.bottomRight.x;
- if(rect.topLeft.y < topLeft.y)
- topLeft.y = rect.topLeft.y;
- if(rect.bottomRight.y > bottomRight.y)
- bottomRight.y = rect.bottomRight.y;
- }
- }
-
- inline void Rect::move(const Point& point)
- {
- topLeft += point;
- bottomRight += point;
- }
-
- inline int Rect::width() const
- {
- int n = bottomRight.x - topLeft.x;
- return (n >= 0) ? n : 0;
- }
-
- inline int Rect::height() const
- {
- int n = bottomRight.y - topLeft.y;
- return (n >= 0) ? n : 0;
- }
-
- inline bool Rect::operator == (const Rect& rect) const
- {
- return bool(topLeft == rect.topLeft && bottomRight == rect.bottomRight);
- }
-
- inline bool Rect::operator != (const Rect& rect) const
- {
- return bool(topLeft != rect.topLeft || bottomRight != rect.bottomRight);
- }
-
- inline Rect& Rect::operator += (const Point& point)
- {
- move(point);
- return *this;
- }
-
- inline Rect& Rect::operator -= (const Point& point)
- {
- move(-point);
- return *this;
- }
-
- inline Rect& Rect::operator |= (const Rect& rect)
- {
- unionWith(rect);
- return *this;
- }
-
- inline Rect& Rect::operator &= (const Rect& rect)
- {
- intersectWith(rect);
- return *this;
- }
-
- // Related operators.
- inline Rect operator + (const Rect& rect, const Point& point)
- {
- Rect r = rect;
- r += point;
- return r;
- }
-
- inline Rect operator - (const Rect& rect, const Point& point)
- {
- Rect r = rect;
- r -= point;
- return r;
- }
-
- inline Rect operator | (const Rect& r1, const Rect& r2)
- {
- Rect r = r1;
- r |= r2;
- return r;
- }
-
- inline Rect operator & (const Rect& r1, const Rect& r2)
- {
- Rect r = r1;
- r &= r2;
- return r;
- }
-
- #endif // _MISC_HPP
-