]> git.sesse.net Git - casparcg/blob - SFML-1.6/include/SFML/Graphics/Rect.inl
(no commit message)
[casparcg] / SFML-1.6 / include / SFML / Graphics / Rect.inl
1 ////////////////////////////////////////////////////////////\r
2 //\r
3 // SFML - Simple and Fast Multimedia Library\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)\r
5 //\r
6 // This software is provided 'as-is', without any express or implied warranty.\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.\r
8 //\r
9 // Permission is granted to anyone to use this software for any purpose,\r
10 // including commercial applications, and to alter it and redistribute it freely,\r
11 // subject to the following restrictions:\r
12 //\r
13 // 1. The origin of this software must not be misrepresented;\r
14 //    you must not claim that you wrote the original software.\r
15 //    If you use this software in a product, an acknowledgment\r
16 //    in the product documentation would be appreciated but is not required.\r
17 //\r
18 // 2. Altered source versions must be plainly marked as such,\r
19 //    and must not be misrepresented as being the original software.\r
20 //\r
21 // 3. This notice may not be removed or altered from any source distribution.\r
22 //\r
23 ////////////////////////////////////////////////////////////\r
24 \r
25 \r
26 ////////////////////////////////////////////////////////////\r
27 /// Default constructor\r
28 ////////////////////////////////////////////////////////////\r
29 template <typename T>\r
30 Rect<T>::Rect() :\r
31 Left  (0),\r
32 Top   (0),\r
33 Right (0),\r
34 Bottom(0)\r
35 {\r
36 \r
37 }\r
38 \r
39 \r
40 ////////////////////////////////////////////////////////////\r
41 /// Construct the color from its coordinates\r
42 ////////////////////////////////////////////////////////////\r
43 template <typename T>\r
44 Rect<T>::Rect(T LeftCoord, T TopCoord, T RightCoord, T BottomCoord) :\r
45 Left  (LeftCoord),\r
46 Top   (TopCoord),\r
47 Right (RightCoord),\r
48 Bottom(BottomCoord)\r
49 {\r
50 \r
51 }\r
52 \r
53 \r
54 ////////////////////////////////////////////////////////////\r
55 /// Get the width of the rectangle\r
56 ////////////////////////////////////////////////////////////\r
57 template <typename T>\r
58 T Rect<T>::GetWidth() const\r
59 {\r
60     return Right - Left;\r
61 }\r
62 \r
63 \r
64 ////////////////////////////////////////////////////////////\r
65 /// Get the height of the rectangle\r
66 ////////////////////////////////////////////////////////////\r
67 template <typename T>\r
68 T Rect<T>::GetHeight() const\r
69 {\r
70     return Bottom - Top;\r
71 }\r
72 \r
73 \r
74 ////////////////////////////////////////////////////////////\r
75 /// Move the whole rectangle by the given offset\r
76 ////////////////////////////////////////////////////////////\r
77 template <typename T>\r
78 void Rect<T>::Offset(T OffsetX, T OffsetY)\r
79 {\r
80     Left   += OffsetX;\r
81     Right  += OffsetX;\r
82     Top    += OffsetY;\r
83     Bottom += OffsetY;\r
84 }\r
85 \r
86 \r
87 ////////////////////////////////////////////////////////////\r
88 /// Check if a point is inside the rectangle's area\r
89 ////////////////////////////////////////////////////////////\r
90 template <typename T>\r
91 bool Rect<T>::Contains(T X, T Y) const\r
92 {\r
93     return (X >= Left) && (X <= Right) && (Y >= Top) && (Y <= Bottom);\r
94 }\r
95 \r
96 \r
97 ////////////////////////////////////////////////////////////\r
98 /// Check intersection between two rectangles\r
99 ////////////////////////////////////////////////////////////\r
100 template <typename T>\r
101 bool Rect<T>::Intersects(const Rect<T>& Rectangle, Rect<T>* OverlappingRect) const\r
102 {\r
103     // Compute overlapping rect\r
104     Rect Overlapping(std::max(Left,   Rectangle.Left),\r
105                      std::max(Top,    Rectangle.Top),\r
106                      std::min(Right,  Rectangle.Right),\r
107                      std::min(Bottom, Rectangle.Bottom));\r
108 \r
109     // If overlapping rect is valid, then there is intersection\r
110     if ((Overlapping.Left < Overlapping.Right) && (Overlapping.Top < Overlapping.Bottom))\r
111     {\r
112         if (OverlappingRect)\r
113             *OverlappingRect = Overlapping;\r
114         return true;\r
115     }\r
116     else\r
117     {\r
118         if (OverlappingRect)\r
119             *OverlappingRect = Rect(0, 0, 0, 0);\r
120         return false;\r
121     }\r
122 }\r