acbcc27e30b3b7bb51a46089a5c168f1d88654a1
[tpg/acess2.git] / Usermode / Applications / axwin4_src / Server / CRect.cpp
1 /*
2  * Acess2 GUI v4
3  * - By John Hodge (thePowersGang)
4  *
5  * CRect.cpp
6  * - Rectangle
7  */
8 #include <CRect.hpp>
9 #include <algorithm>
10 #include <acess/sys.h>
11
12 namespace AxWin {
13
14 CRect::CRect(int x, int y, unsigned int w, unsigned int h):
15         m_x(x), m_y(y),
16         m_w(w), m_h(h),
17         m_x2(x+w), m_y2(y+h)
18 {
19 }
20
21 void CRect::Move(int NewX, int NewY)
22 {
23         // TODO: Add a parent rectangle, and prevent this from fully leaving its bounds
24         m_x = NewX;
25         m_y = NewY;
26         m_x2 = m_x + m_w;
27         m_y2 = m_y + m_h;
28 }
29
30 void CRect::Resize(int NewW, int NewH)
31 {
32         m_w = NewW;
33         m_h = NewH;
34         m_x2 = m_x + m_w;
35         m_y2 = m_y + m_h;
36 }
37
38 bool CRect::HasIntersection(const CRect& other) const
39 {
40         // If other's origin is past our far corner
41         if( m_x2 < other.m_x )
42                 return false;
43         if( m_y2 < other.m_y )
44                 return false;
45         
46         // If other's far corner is before our origin
47         if( m_x > other.m_x2 )
48                 return false;
49         if( m_y > other.m_y2 )
50                 return false;
51         return true;
52 }
53
54 CRect CRect::Intersection(const CRect& other) const
55 {
56         int x1 = ::std::max(m_x, other.m_x);
57         int y1 = ::std::max(m_y, other.m_y);
58         int x2 = ::std::min(m_x2, other.m_x2);
59         int y2 = ::std::min(m_y2, other.m_y2);
60         
61         if( x2 <= x1 || y2 <= y1 )
62                 return CRect();
63         
64         return CRect(x1, y1, x2-x1, y2-y1);
65 }
66
67 CRect CRect::RelativeIntersection(const CRect& area)
68 {
69         CRect   ret = Intersection(area);
70         ret.m_x -= m_x;
71         ret.m_x2 -= m_x;
72         ret.m_y -= m_y;
73         ret.m_y2 -= m_y;
74         return ret;
75 }
76
77 };
78

UCC git Repository :: git.ucc.asn.au