wxRealRect class implementation

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
orbitcowboy
I live to help wx-kind
I live to help wx-kind
Posts: 178
Joined: Mon Jul 23, 2007 9:01 am

wxRealRect class implementation

Post by orbitcowboy » Tue Apr 22, 2008 9:49 am

<!-- --><!-- --><!-- -->Hi @all,

i've developed a wxRealRect class dealing with wxRealPoints.
:-)

Here the code:


wxRealRect.h:

Code: Select all

#ifndef __class__wxRealRectangle__
#define __class__wxRealRectangle__

#include <wx/wx.h>


class wxRealRect
{
public:

    wxRealRect()
        : x(0.0), y(0.0), width(0.0), height(0.0)
        { }
    wxRealRect(const double &xx, const double &yy, const double &ww, const double &hh)
        : x(xx), y(yy), width(ww), height(hh)
        { }
    wxRealRect(const wxRealPoint& topLeft, const wxRealPoint& bottomRight);
    wxRealRect(const wxRealPoint& pt, const wxSize& size)
        : x(pt.x), y(pt.y), width(size.x), height(size.y)
        { }
    wxRealRect(const wxSize& size)
        : x(0.0), y(0.0), width(size.x), height(size.y)
        { }

    // default copy ctor and assignment operators ok

    inline double GetX() const  { return x; }
    inline void SetX(const double &xx) { x = xx; 	}

    inline double GetY() const  { return y; }
    inline void SetY(const double &yy) { y = yy; 	}

    inline double GetWidth() const { return width; }
    inline void SetWidth(const double &w) { width = w; }

    inline double GetHeight() const { return height;  }
    inline void SetHeight(const double &h) { height = h; 	  }

    inline wxRealPoint GetPosition() const { return wxRealPoint(x, y); }
    inline void SetPosition( const wxRealPoint &p ) { x = p.x; y = p.y; }

    inline wxSize GetSize() const { return wxSize(width, height); }
    inline void SetSize( const wxSize &s ) { width = s.GetWidth(); height = s.GetHeight(); }

    inline bool IsEmpty() const { return (width <= 0.0) || (height <= 0.0); }

    inline double GetLeft()   const { return x; }
    inline double GetTop()	const { return y; }
    inline double GetBottom() const { return y + height - 1.0; }
    inline double GetRight()  const { return x + width - 1.0; }

    inline void SetLeft(const double &left) { x = left; }
    inline void SetRight(const double &right) { width = right - x + 1; }
    inline void SetTop(const double &top) { y = top; }
    inline void SetBottom(const double &bottom) { height = bottom - y + 1; }

    inline wxRealPoint GetTopLeft() const { return GetPosition(); }
    inline wxRealPoint GetLeftTop() const { return GetTopLeft(); }
    inline void SetTopLeft(const wxRealPoint &p) { SetPosition(p); }
    inline void SetLeftTop(const wxRealPoint &p) { SetTopLeft(p); }

    inline wxRealPoint GetBottomRight() const { return wxRealPoint(GetRight(), GetBottom()); }
    inline wxRealPoint GetRightBottom() const { return GetBottomRight(); }
    inline void SetBottomRight(const wxRealPoint &p) { SetRight(p.x); SetBottom(p.y); }
    inline void SetRightBottom(const wxRealPoint &p) { SetBottomRight(p); }

    inline wxRealPoint GetTopRight() const { return wxRealPoint(GetRight(), GetTop()); }
    inline wxRealPoint GetRightTop() const { return GetTopRight(); }
    inline void SetTopRight(const wxRealPoint &p) { SetRight(p.x); SetTop(p.y); }
    inline void SetRightTop(const wxRealPoint &p) { SetTopLeft(p); }

    inline wxRealPoint GetBottomLeft() const { return wxRealPoint(GetLeft(), GetBottom()); }
    inline wxRealPoint GetLeftBottom() const { return GetBottomLeft(); }
    inline void SetBottomLeft(const wxRealPoint &p) { SetLeft(p.x); SetBottom(p.y); }
    inline void SetLeftBottom(const wxRealPoint &p) { SetBottomLeft(p); }

    // operations with rect
	wxRealRect& Inflate(wxCoord dx, wxCoord dy);
	inline wxRealRect& Inflate(const wxSize& d) { return Inflate(d.x, d.y); }
	inline wxRealRect& Inflate(wxCoord d) { return Inflate(d, d); }
	inline wxRealRect Inflate(wxCoord dx, wxCoord dy) const
	{
		wxRealRect r = *this;
		r.Inflate(dx, dy);
		return r;
	}

  	inline wxRealRect& Deflate(wxCoord dx, wxCoord dy) { return Inflate(-dx, -dy); }
  	inline wxRealRect& Deflate(const wxSize& d) { return Inflate(-d.x, -d.y); }
  	inline wxRealRect& Deflate(wxCoord d) { return Inflate(-d); }
  	inline wxRealRect Deflate(wxCoord dx, wxCoord dy) const
  	{
	  	wxRealRect r = *this;
	  	r.Deflate(dx, dy);
	  	return r;
	 }

   inline  void Offset(const wxCoord &dx, const wxCoord &dy) { x += dx; y += dy; }
   inline   void Offset(const wxRealPoint& pt) { Offset(pt.x, pt.y); }

   wxRealRect& Intersect(const wxRealRect& rect);
   wxRealRect Intersect(const wxRealRect& rect) const
   {
	  wxRealRect r = *this;
	  r.Intersect(rect);
	  return r;
   }

   wxRealRect& Union(const wxRealRect& rect);
   wxRealRect Union(const wxRealRect& rect) const
   {
	  wxRealRect r = *this;
	  r.Union(rect);
	  return r;
   }



    // compare rectangles
    bool operator==(const wxRealRect& rect) const;
    inline bool operator!=(const wxRealRect& rect) const { return !(*this == rect); }

    // return true if the point is (not strcitly) inside the rect
  	bool Contains(const double &x, const double &y) const;
	inline bool Contains(const wxRealPoint& pt) const { return Contains(pt.x, pt.y); }
    // return true if the rectangle is (not strcitly) inside the rect
    bool Contains(const wxRealRect& rect) const;


    // return true if the rectangles have a non empty intersection
    bool Intersects(const wxRealRect& rect) const;


    // these are like Union() but don't ignore empty rectangles
    wxRealRect operator+(const wxRealRect& rect) const;
  	wxRealRect& operator+=(const wxRealRect& rect)
  	{
	  *this = *this + rect;
	  return *this;
  	}


    // centre this rectangle in the given (usually, but not necessarily,
    // larger) one
    wxRealRect CentreIn(const wxRealRect& r, int dir = wxBOTH) const
    {
        return wxRealRect(dir & wxHORIZONTAL ? r.x + (r.width - width)/2 : x,
                      	  dir & wxVERTICAL ? r.y + (r.height - height)/2 : y,
                      	  width, height);
    }

    inline wxRealRect CenterIn(const wxRealRect& r, int dir = wxBOTH) const
    {
	   return CentreIn(r, dir);
    }



public:
    double x, y, width, height;
};


#endif 

wxRealRect.cpp

Code: Select all

#include "wxRealRect.h"

wxRealRect::wxRealRect(const wxRealPoint& point1, const wxRealPoint& point2)
{
    x = point1.x;
    y = point1.y;
    width = point2.x - point1.x;
    height = point2.y - point1.y;

    if (width < 0)
    {
        width = -width;
        x = point2.x;
    }
    width++;

    if (height < 0)
    {
        height = -height;
        y = point2.y;
    }
    height++;
}

bool wxRealRect::operator==(const wxRealRect& rect) const
{
    return ((x == rect.x) &&
            (y == rect.y) &&
            (width == rect.width) &&
            (height == rect.height));
}

wxRealRect wxRealRect::operator+(const wxRealRect& rect) const
{
    int x1 = wxMin(this->x, rect.x);
    int y1 = wxMin(this->y, rect.y);
    int y2 = wxMax(y+height, rect.height+rect.y);
    int x2 = wxMax(x+width, rect.width+rect.x);
    return wxRealRect(x1, y1, x2-x1, y2-y1);
}

wxRealRect& wxRealRect::Union(const wxRealRect& rect)
{
    // ignore empty rectangles: union with an empty rectangle shouldn't extend
    // this one to (0, 0)
    if ( !width || !height )
    {
        *this = rect;
    }
    else if ( rect.width && rect.height )
    {
        int x1 = wxMin(x, rect.x);
        int y1 = wxMin(y, rect.y);
        int y2 = wxMax(y + height, rect.height + rect.y);
        int x2 = wxMax(x + width, rect.width + rect.x);

        x = x1;
        y = y1;
        width = x2 - x1;
        height = y2 - y1;
    }
    //else: we're not empty and rect is empty

    return *this;
}

wxRealRect& wxRealRect::Inflate(wxCoord dx, wxCoord dy)
{
     if (-2*dx>width)
     {
         // Don't allow deflate to eat more width than we have,
         // a well-defined rectangle cannot have negative width.
         x+=width/2;
         width=0;
     }
     else
     {
         // The inflate is valid.
         x-=dx;
         width+=2*dx;
     }

     if (-2*dy>height)
     {
         // Don't allow deflate to eat more height than we have,
         // a well-defined rectangle cannot have negative height.
         y+=height/2;
         height=0;
     }
     else
     {
         // The inflate is valid.
         y-=dy;
         height+=2*dy;
     }

    return *this;
}

bool wxRealRect::Contains(const double &cx, const double &cy) const
{
    return ( (cx >= x) && (cy >= y)
          && ((cy - y) < height)
          && ((cx - x) < width)
          );
}

bool wxRealRect::Contains(const wxRealRect& rect) const
{
    return Contains(rect.GetTopLeft()) && Contains(rect.GetBottomRight());
}

wxRealRect& wxRealRect::Intersect(const wxRealRect& rect)
{
    int x2 = GetRight(),
        y2 = GetBottom();

    if ( x < rect.x )
        x = rect.x;
    if ( y < rect.y )
        y = rect.y;
    if ( x2 > rect.GetRight() )
        x2 = rect.GetRight();
    if ( y2 > rect.GetBottom() )
        y2 = rect.GetBottom();

    width = x2 - x + 1;
    height = y2 - y + 1;

    if ( width <= 0 || height <= 0 )
    {
        width =
        height = 0;
    }

    return *this;
}

bool wxRealRect::Intersects(const wxRealRect& rect) const
{
    wxRealRect r = Intersect(rect);

    // if there is no intersection, both width and height are 0
    return r.width != 0;
}



Regards

Orbitcowboy
Attachments
wxRealRect.tar.gz
(2.39 KiB) Downloaded 92 times

orbitcowboy
I live to help wx-kind
I live to help wx-kind
Posts: 178
Joined: Mon Jul 23, 2007 9:01 am

Post by orbitcowboy » Wed Apr 23, 2008 1:14 pm

<!-- -->According to a few comments (in wx-dev mailing list) i have implemented a the wxPoint , wxSize and wxRect classes as C++ templates ....


here the source code:
wxPoint.h:

Code: Select all

#ifndef __class_wxPoint__template__
#define __class_wxPoint__template__

#include "wxSize.h"

template <class T> class wxPoint
{
public:
	T x, y;

	wxPoint() : x(0), y(0){}
	wxPoint(const T &xx, const T &yy) : x(xx), y(yy){}
	wxPoint(const wxPoint<T> &p)			  {x=p.x; y=p.y;			  };
	wxPoint<T>& operator=(const wxPoint<T> &p){x=p.x; y=p.y; return *this;};

	// comparison
	inline bool operator==(const wxPoint<T>& p) const { return (x == p.x) && (y == p.y); }
	inline bool operator!=(const wxPoint<T>& p) const { return !(*this == p); }

	// arithmetic operations (component wise)
	inline wxPoint<T> operator+(const wxPoint<T>& p) const { return wxPoint<T>(x + p.x, y + p.y); }
	inline wxPoint<T> operator-(const wxPoint<T>& p) const { return wxPoint<T>(x - p.x, y - p.y); }

	inline wxPoint<T>& operator+=(const wxPoint<T>& p) { x += p.x; y += p.y; return *this; }
	inline wxPoint<T>& operator-=(const wxPoint<T>& p) { x -= p.x; y -= p.y; return *this; }

	inline wxPoint<T>& operator+=(const wxSize<T>& s) { x += s.GetWidth(); y += s.GetHeight(); return *this; }
	inline wxPoint<T>& operator-=(const wxSize<T>& s) { x -= s.GetWidth(); y -= s.GetHeight(); return *this; }

	inline wxPoint<T> operator+(const wxSize<T>& s) const { return wxPoint<T>(x + s.GetWidth(), y + s.GetHeight()); }
	inline wxPoint<T> operator-(const wxSize<T>& s) const { return wxPoint<T>(x - s.GetWidth(), y - s.GetHeight()); }

	wxPoint<T> operator-() const { return wxPoint<T>(-x, -y); }
};


#endif // __class_wxPoint__template__


wxSize.h:

Code: Select all

#ifndef __class_wxSize__template__
#define __class_wxSize__template__

//typedef int wxCoord;

enum {  wxDefaultCoord = -1 };

template <class T> class wxSize
{
public:
    // members are public for compatibility, don't use them directly.
    T x, y;

    // constructors
    wxSize() : x(0), y(0){}
    wxSize(const T &xx, const T &yy) : x(xx), y(yy){}
	wxSize(const wxSize<T> &sz) 		   {x=sz.x; y=sz.y;					};
	wxSize& operator=(const wxSize<T> &sz) {x=sz.x; y=sz.y; return *this;	};

    inline bool operator==(const wxSize<T>& sz) const { return (x == sz.x) && (y == sz.y); };
    inline bool operator!=(const wxSize<T>& sz) const { return (x != sz.x) || (y != sz.y); };

    inline wxSize<T> operator+(const wxSize<T>& sz) const { return wxSize<T>(x + sz.x, y + sz.y); };
    inline wxSize<T> operator-(const wxSize<T>& sz) const { return wxSize<T>(x - sz.x, y - sz.y); };
    inline wxSize<T> operator/(const T &i) const 		  { return wxSize<T>(x / i, y / i); };
    inline wxSize<T> operator*(const T &i) const 		  { return wxSize<T>(x * i, y * i); };

    inline wxSize<T>& operator+=(const wxSize<T>& sz) { x += sz.x; y += sz.y; return *this; };
    inline wxSize<T>& operator-=(const wxSize<T>& sz) { x -= sz.x; y -= sz.y; return *this; };
    inline wxSize<T>& operator/=(const T &i) 		  { x /= i; y /= i; return *this; };
    inline wxSize<T>& operator*=(const T &i) 		  { x *= i; y *= i; return *this; };

    inline void IncTo(const wxSize<T>& sz) { if ( sz.x > x ) x = sz.x; if ( sz.y > y ) y = sz.y; }
    inline void DecTo(const wxSize<T>& sz) { if ( sz.x < x ) x = sz.x; if ( sz.y < y ) y = sz.y; }

    inline void IncBy(const T &dx, const T &dy) { x += dx; y += dy;    }
    inline void IncBy(const wxSize& sz) 		{ IncBy(sz.x, sz.y);   }
    inline void IncBy(const T &d)				{ IncBy(d, d);  	   }

    inline void DecBy(const T &dx, const T &dy) { IncBy(-dx, -dy);     }
    inline void DecBy(const wxSize& sz) 		{ DecBy(sz.x, sz.y);   }
    inline void DecBy(const T &d)				{ DecBy(d, d);  	   }


    wxSize<T>& Scale(const float &xscale, const float &yscale){ x = static_cast<T>(x*xscale); y = static_cast<T>(y*yscale); return *this; }

    // accessors
    inline void Set(const T &xx, const T &yy) { x = xx; y = yy; }
    inline void SetWidth(const T &w)  { x = w; }
    inline void SetHeight(const T &h) { y = h; }

    inline T GetWidth() const  { return x; }
    inline T GetHeight() const { return y; }

    inline bool IsFullySpecified() const { return x != wxDefaultCoord && y != wxDefaultCoord; }

    // combine this size with the other one replacing the default (i.e. equal
    // to wxDefaultCoord) components of this object with those of the other
    inline void SetDefaults(const wxSize<T>& size){if (x == wxDefaultCoord)x = size.x; if(y==wxDefaultCoord)y = size.y;};

    // compatibility
    inline T GetX() const { return x; }
    inline T GetY() const { return y; }
};



#endif // __class_wxSize__template__


wxRect.h

Code: Select all

#ifndef __class__wxRect__template__
#define __class__wxRect__template__

#include "wxPoint.h"
#include "wxSize.h"

enum wxOrientation
{
    /* don't change the values of these elements, they are used elsewhere */
    wxHORIZONTAL              = 0x0004,
    wxVERTICAL                = 0x0008,

    wxBOTH                    = wxVERTICAL | wxHORIZONTAL
};

template <class T> class wxRect
{
public:

    wxRect()
        : x(0.0), y(0.0), width(0.0), height(0.0){};

    wxRect(const T &xx, const T &yy, const T &ww, const T &hh)
        : x(xx), y(yy), width(ww), height(hh){};

    wxRect(const wxPoint<T>& topLeft, const wxPoint<T>& bottomRight);

    wxRect(const wxPoint<T>& pt, const wxSize<T>& size)
        : x(pt.x), y(pt.y), width(size.x), height(size.y){};

    wxRect(const wxSize<T>& size)
        : x(0.0), y(0.0), width(size.x), height(size.y){}

    // default copy ctor and assignment operators ok

    inline T GetX() const   	  { return x; 	}
    inline void SetX(const T &xx) { x = xx; 	}

    inline T GetY() const   		    { return y; }
    inline void SetY(const T &yy) 		{ y = yy; 	}

    inline T 	GetWidth() const 	 { return width; }
    inline void SetWidth(const T &w) { width = w; }

    inline T GetHeight() const  	  { return height;  }
    inline void SetHeight(const T &h) { height = h; 	  }

    inline wxPoint<T> GetPosition() const 		   { return wxPoint<T>(x, y); }
    inline void SetPosition( const wxPoint<T> &p ) { x = p.x; y = p.y;  	  }

    inline wxSize<T> GetSize() const { return wxSize<T>(width, height); }
    inline void SetSize( const wxSize<T> &s ) { width = s.GetWidth(); height = s.GetHeight(); }

    inline bool IsEmpty() const { return (width <= static_cast<T>(0.0)) || (height <= static_cast<T>(0.0)); }

    inline T GetLeft()   const { return x; }
    inline T GetTop()	 const { return y; }
    inline T GetBottom() const { return y + height - static_cast<T>(1.0); }
    inline T GetRight()  const { return x + width  - static_cast<T>(1.0); }

    inline void SetLeft(const T &left) { x = left; }
    inline void SetRight(const T &right) { width = right - x + static_cast<T>(1.0); }
    inline void SetTop(const T &top) { y = top; }
    inline void SetBottom(const T &bottom) { height = bottom - y + static_cast<T>(1.0); }

    inline wxPoint<T> GetTopLeft() const { return GetPosition(); }
    inline wxPoint<T> GetLeftTop() const { return GetTopLeft(); }
    inline void SetTopLeft(const wxPoint<T> &p) { SetPosition(p); }
    inline void SetLeftTop(const wxPoint<T> &p) { SetTopLeft(p); }

    inline wxPoint<T> GetBottomRight() const { return wxPoint<T>(GetRight(), GetBottom()); }
    inline wxPoint<T> GetRightBottom() const { return GetBottomRight(); }
    inline void SetBottomRight(const wxPoint<T> &p) { SetRight(p.x); SetBottom(p.y); }
    inline void SetRightBottom(const wxPoint<T> &p) { SetBottomRight(p); }

    inline wxPoint<T> GetTopRight() const { return wxPoint<T>(GetRight(), GetTop()); }
    inline wxPoint<T> GetRightTop() const { return GetTopRight(); }
    inline void SetTopRight(const wxPoint<T> &p) { SetRight(p.x); SetTop(p.y); }
    inline void SetRightTop(const wxPoint<T> &p) { SetTopLeft(p); }

    inline wxPoint<T> GetBottomLeft() const { return wxPoint<T>(GetLeft(), GetBottom()); }
    inline wxPoint<T> GetLeftBottom() const { return GetBottomLeft(); }
    inline void SetBottomLeft(const wxPoint<T> &p) { SetLeft(p.x); SetBottom(p.y); }
    inline void SetLeftBottom(const wxPoint<T> &p) { SetBottomLeft(p); }

    // operations with rect
	wxRect<T>& Inflate(const T &dx, const T &dy);
	inline wxRect<T>& Inflate(const wxSize<T>& d) { return Inflate(d.x, d.y);}
	inline wxRect<T>& Inflate(const T &d) { return Inflate(d, d); }
	inline wxRect<T>  Inflate(const T &dx, const T &dy) const
	{
		wxRect<T> r = *this;
		r.Inflate(dx, dy);
		return r;
	}

  	inline wxRect<T>& Deflate(const T &dx, const T &dy) { return Inflate(-dx, -dy); }
  	inline wxRect<T>& Deflate(const wxSize<T>& d) { return Inflate(-d.x, -d.y); }
  	inline wxRect<T>& Deflate(const T &d) 		{ return Inflate(-d); }
  	inline wxRect<T>  Deflate(const T &dx, const T &dy) const
  	{
	  	wxRect<T> r = *this;
	  	r.Deflate(dx, dy);
	  	return r;
	 }

   inline  void Offset(const T &dx, const T &dy) { x += dx; y += dy; }
   inline  void Offset(const wxPoint<T>& pt) { Offset(pt.x, pt.y); }

   wxRect<T>& Intersect(const wxRect<T>& rect);
   wxRect<T>  Intersect(const wxRect<T>& rect) const
   {
	  wxRect r = *this;
	  r.Intersect(rect);
	  return r;
   }

   wxRect<T>& Union(const wxRect<T>& rect);
   wxRect<T> Union(const wxRect<T>& rect) const
   {
	  wxRect<T> r = *this;
	  r.Union(rect);
	  return r;
   }



    // compare rectangles
    inline bool operator==(const wxRect<T>& rect) const;
    inline bool operator!=(const wxRect<T>& rect) const { return !(*this == rect); }

    // return true if the point is (not strcitly) inside the rect
  	inline bool Contains(const T &x, const T &y) const;
	inline bool Contains(const wxPoint<T>& pt) const { return Contains(pt.x, pt.y); }
    // return true if the rectangle is (not strcitly) inside the rect
    bool Contains(const wxRect<T>& rect) const;


    // return true if the rectangles have a non empty intersection
    bool Intersects(const wxRect<T>& rect) const;


    // these are like Union() but don't ignore empty rectangles
    wxRect<T> operator+(const wxRect<T>& rect) const;
  	wxRect<T>& operator+=(const wxRect<T>& rect)
  	{
	  *this = *this + rect;
	  return *this;
  	}


    // centre this rectangle in the given (usually, but not necessarily,
    // larger) one
    wxRect<T> CentreIn(const wxRect<T>& r, const T &dir = wxBOTH) const
    {
        return wxRect<T>(dir & wxHORIZONTAL ? r.x + (r.width - width)/2 : x,
                      	  dir & wxVERTICAL ? r.y + (r.height - height)/2 : y,
                      	  width, height);
    }

    inline wxRect<T> CenterIn(const wxRect<T>& r, const T &dir = wxBOTH) const
    {
	   return CentreIn(r, dir);
    }



public:
    T x, y, width, height;
};



template <class T> wxRect<T>::wxRect(const wxPoint<T>& point1, const wxPoint<T>& point2)
{
    x = point1.x;
    y = point1.y;
    width = point2.x - point1.x;
    height = point2.y - point1.y;

    if (width < 0)
    {
        width = -width;
        x = point2.x;
    }
    width++;

    if (height < 0)
    {
        height = -height;
        y = point2.y;
    }
    height++;
}

template <class T> bool wxRect<T>::operator==(const wxRect<T>& rect) const
{
    return ((x == rect.x) &&
            (y == rect.y) &&
            (width == rect.width) &&
            (height == rect.height));
}

template <class T> wxRect<T> wxRect<T>::operator+(const wxRect<T>& rect) const
{
    T x1 = wxMin(this->x, rect.x);
    T y1 = wxMin(this->y, rect.y);
    T y2 = wxMax(y+height, rect.height+rect.y);
    T x2 = wxMax(x+width, rect.width+rect.x);
    return wxRect(x1, y1, x2-x1, y2-y1);
}

template <class T> wxRect<T>& wxRect<T>::Union(const wxRect<T>& rect)
{
    // ignore empty rectangles: union with an empty rectangle shouldn't extend
    // this one to (0, 0)
    if ( !width || !height )
    {
        *this = rect;
    }
    else if ( rect.width && rect.height )
    {
        T x1 = wxMin(x, rect.x);
        T y1 = wxMin(y, rect.y);
        T y2 = wxMax(y + height, rect.height + rect.y);
        T x2 = wxMax(x + width, rect.width + rect.x);

        x = x1;
        y = y1;
        width = x2 - x1;
        height = y2 - y1;
    }
    //else: we're not empty and rect is empty

    return *this;
}

template <class T> wxRect<T>& wxRect<T>::Inflate(const T &dx, const T &dy)
{
     if (-2*dx>width)
     {
         // Don't allow deflate to eat more width than we have,
         // a well-defined rectangle cannot have negative width.
         x+=width/2;
         width=0;
     }
     else
     {
         // The inflate is valid.
         x-=dx;
         width+=2*dx;
     }

     if (-2*dy>height)
     {
         // Don't allow deflate to eat more height than we have,
         // a well-defined rectangle cannot have negative height.
         y+=height/2;
         height=0;
     }
     else
     {
         // The inflate is valid.
         y-=dy;
         height+=2*dy;
     }

    return *this;
}

template <class T> bool wxRect<T>::Contains(const T &cx, const T &cy) const
{
    return ( (cx >= x) && (cy >= y)
          && ((cy - y) < height)
          && ((cx - x) < width)
          );
}

template <class T> bool wxRect<T>::Contains(const wxRect<T>& rect) const
{
    return Contains(rect.GetTopLeft()) && Contains(rect.GetBottomRight());
}

template <class T> wxRect<T>& wxRect<T>::Intersect(const wxRect<T>& rect)
{
    T   x2 = GetRight(),
        y2 = GetBottom();

    if ( x < rect.x )
        x = rect.x;
    if ( y < rect.y )
        y = rect.y;
    if ( x2 > rect.GetRight() )
        x2 = rect.GetRight();
    if ( y2 > rect.GetBottom() )
        y2 = rect.GetBottom();

    width = x2 - x + 1;
    height = y2 - y + 1;

    if ( width <= 0 || height <= 0 )
    {
        width =
        height = 0;
    }

    return *this;
}

template <class T> bool wxRect<T>::Intersects(const wxRect<T>& rect) const
{
    wxRect<T> r = Intersect(rect);

    // if there is no intersection, both width and height are 0
    return r.width != 0;
}

#endif 


Regards

Orbitcowboy

User avatar
doublemax
Moderator
Moderator
Posts: 14875
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax » Wed Apr 23, 2008 2:12 pm

i think all places where you use "+1" or "-1" are wrong, they apply to integer rects only.

And BTW there is already wxRect2DDouble in wx/geometry.h
Use the source, Luke!

Post Reply