namespace ltl
{
template <class T>
void swap(T &a, T &b) { T c = a; a = b; b = c; }
class CSize:public SIZE
{
public:
CSize(){}
CSize(int lx, int ly) { cx = lx, cy = ly; }
void Set(int lx, int ly) { cx = lx, cy = ly; }
operator += (const SIZE &sz) { cx += sz.cx, cy += sz.cy; }
operator -= (const SIZE &sz) { cx -= sz.cx, cy -= sz.cy; }
CSize operator + (const SIZE &sz) const
{
return CSize(cx+sz.cx, cy+sz.cy);
}
CSize operator - (const SIZE &sz) const
{
return CSize(cx-sz.cx, cy-sz.cy);
}
operator = (const SIZE &sz) { cx = sz.cx, cy = sz.cy; }
BOOL operator == (const SIZE &sz) const
{
return cx==sz.cx && cy==sz.cy ? TRUE: FALSE;
}
BOOL operator != (const SIZE &sz) const
{
return cx==sz.cx && cy==sz.cy ? FALSE: TRUE;
}
};
class CPoint : public POINT
{
public:
CPoint() {}
CPoint(int lx, int ly) { x = lx, y = ly; }
void Set(int lx, int ly) { x = lx, y = ly; }
operator += (const POINT &pt) { x += pt.x, y += pt.y; }
operator -= (const POINT &pt) { x -= pt.x, y -= pt.y; }
operator += (const SIZE &sz) { x += sz.cx, y += sz.cy; }
operator -= (const SIZE &sz) { x -= sz.cx, y -= sz.cy; }
CPoint operator + (const POINT &pt) const
{
return CPoint(x+pt.x, y+pt.y);
}
CPoint operator - (const POINT &pt) const
{
return CPoint(x-pt.x, y-pt.y);
}
operator = (const CPoint &pt) { x = pt.x, y = pt.y; }
BOOL operator == (const POINT &pt) const
{
return x==pt.x && y==pt.y ? TRUE: FALSE;
}
BOOL operator != (const POINT &pt) const
{
return x==pt.x && y==pt.y ? FALSE: TRUE;
}
};
class CRect : public RECT
{
public:
/*********************constructor******************************/
CRect() {}
//指定四点
CRect(int lleft, int ltop, int lright, int lbottom)
{
left = lleft, top = ltop, right = lright, bottom = lbottom;
}
//指定长宽,左上角为0, 0
CRect(int Width, int Height)
{
left = 0, top = 0, right = Width, bottom = Height;
}
//指定左上角和右下角
CRect(POINT <, POINT &rb)
{
left = lt.x, top = lt.y, right = rb.x, bottom = rb.y;
}
//指定左上角和长、宽
CRect(POINT &pt, SIZE &sz)
{
left = pt.x, top = pt.y, right = sz.cx+left, bottom = sz.cy+top;
}
/*********************Operations******************************/
int Width() const { return right - left; }
int Height() const { return bottom - top; }
CSize Size() const { return CSize(Width(), Height()); }
CPoint &TopLeft() { return *(CPoint *)this; }
const CPoint &TopLeft() const { return *(CPoint *)this; }
CPoint &BottomRight() { return *((CPoint *)this+1); }
const CPoint &BottomRight() const { return *((CPoint *)this+1); }
CPoint CenterPoint() const { return CPoint((left+right)/2, (top+bottom)/2); }
void SetRect(int lleft, int ltop, int lright, int lbottom)
{
left = lleft, top = ltop, right = lright, bottom = lbottom;
}
//移动矩形到点(左上角点为基准)
void MoveTo(const POINT &pt)
{
SetRect(pt.x, pt.y, pt.x+Width(), pt.y+Height());
}
//判断点是否在矩形中
BOOL PtInRect(const POINT &pt) const
{
return pt.x<min(left, right)||pt.x>max(left, right)||
pt.y<min(top, bottom)||pt.y>max(top, bottom) ? FALSE : TRUE;
}
//长宽为正?
BOOL IsNormal() const
{
return right<left || bottom<top ? FALSE : TRUE;
}
BOOL IsNull() const
{
return left==0 && top==0 && right==0 && bottom==0 ? TRUE : FALSE;
}
BOOL IsEmpty() const
{
return Width()==0 && Height()==0 ? TRUE : FALSE;
}
//正常化
void NormalizeRect()
{
if (right<left) swap(right, left);
if (bottom<top) swap(top, bottom);
}
/*********************operator************************************/
operator = (const RECT &rt)
{
left = rt.left, top = rt.top, right = rt.right, bottom = rt.bottom;
}
BOOL operator == (const RECT &rt) const
{
return left==rt.left && top==rt.top && right==rt.right && bottom==rt.bottom ? TRUE : FALSE;
}
BOOL operator != (const RECT &rt) const
{
return left==rt.left && top==rt.top && right==rt.right && bottom==rt.bottom ? FALSE : TRUE;
}
//由中心扩张
operator += (int inc)
{
left-=inc, top-=inc, right+=inc, bottom+=inc;
}
operator -= (int inc)
{
left+=inc, top+=inc, right-=inc, bottom-=inc;
}
CRect operator + (int inc) const
{
return CRect(left-inc, top-inc, right+inc, bottom+inc);
}
CRect operator - (int inc) const
{
return CRect(left+inc, top+inc, right-inc, bottom-inc);
}
//偏移
operator += (const POINT &pt)
{
left+=pt.x, top+=pt.y, right+=pt.x, bottom+=pt.y;
}
operator -= (const POINT &pt)
{
left-=pt.x, top-=pt.y, right-=pt.x, bottom-=pt.y;
}
CRect operator + (const POINT &pt) const
{
return CRect(left+pt.x, top+pt.y, right+pt.x, bottom+pt.y);
}
CRect operator - (const POINT &pt) const
{
return CRect(left-pt.x, top-pt.y, right-pt.x, bottom-pt.y);
}
//由左上角扩展
operator += (const SIZE &sz)
{
right+=sz.cx, bottom+=sz.cy;
}
operator -= (const SIZE &sz)
{
right-=sz.cx, bottom-=sz.cy;
}
CRect operator + (const SIZE &sz) const
{
return CRect(left, top, right+sz.cx, bottom+sz.cy);
}
CRect operator - (const SIZE &sz) const
{
return CRect(left, top, right-sz.cx, bottom-sz.cy);
}
//增量
operator += (const RECT &rt)
{
left+=rt.left, top+=rt.top, right+=rt.right, bottom+=rt.bottom;
}
operator -= (const RECT &rt)
{
left-=rt.left, top-=rt.top, right-=rt.right, bottom-=rt.bottom;
}
CRect operator + (const RECT &rt) const
{
return CRect(left+rt.left, top+rt.top, right+rt.right, bottom+rt.bottom);
}
CRect operator - (const RECT &rt) const
{
return CRect(left-rt.left, top-rt.top, right-rt.right, bottom-rt.bottom);
}
//取交集
operator &= (const RECT &rt)
{
left = max(min(left, right), min(rt.left, rt.right));
top = max(min(top, bottom), min(rt.top, rt.bottom));
right = min(max(left, right), max(rt.left, rt.right));
bottom = min(max(top, bottom), max(rt.top, rt.bottom));
if (!IsNormal()) SetRect(0, 0, 0, 0);
}
CRect operator & (const RECT &rt) const
{
CRect rt1(
max(min(left, right), min(rt.left, rt.right)),
max(min(top, bottom), min(rt.top, rt.bottom)),
min(max(left, right), max(rt.left, rt.right)),
min(max(top, bottom), max(rt.top, rt.bottom))
);
if (!rt1.IsNormal()) rt1.SetRect(0, 0, 0, 0);
return rt1;
}
//取并集
operator |= (const RECT &rt)
{
left = min(min(left, right), min(rt.left, rt.right));
top = min(min(top, bottom), min(rt.top, rt.bottom));
right = max(max(left, right), max(rt.left, rt.right));
bottom = max(max(top, bottom), max(rt.top, rt.bottom));
}
CRect operator | (const RECT &rt) const
{
return CRect(
min(min(left, right), min(rt.left, rt.right)),
min(min(top, bottom), min(rt.top, rt.bottom)),
max(max(left, right), max(rt.left, rt.right)),
max(max(top, bottom), max(rt.top, rt.bottom))
);
}
private:
};
} //end of namespace