Tizen 2.1 base
[framework/osp/uifw.git] / src / graphics / FGrpFloatRectangle.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /*
19  * @file        FGrpFloatRectangle.cpp
20  * @brief       This is the implementation file for FloatRectangle class.
21  *
22  * This file contains implementation of FloatRectangle class.
23  *
24  */
25
26 #include <FGrpFloatRectangle.h>
27 #include <FGrpFloatPoint.h>
28 #include <FGrpFloatDimension.h>
29
30 #include <FBaseSysLog.h>
31
32 #include "util/FGrp_Util.h"
33
34
35 namespace // unnamed
36 {
37
38 const float _EMPTY = -1.0f;
39
40 // epsilon is assumed to be 0.00001f, not 1.192092896e-07f
41 const float _EPSILON = 0.00001f;
42
43 inline bool
44 _IsEqual(float f1, float f2)
45 {
46         return (((f1 > f2) ? f1 - f2 : f2 - f1) < _EPSILON);
47 }
48
49 }
50
51 namespace Tizen { namespace Graphics
52 {
53
54 FloatRectangle::FloatRectangle()
55         : x(0.0f)
56         , y(0.0f)
57         , width(_EMPTY)
58         , height(_EMPTY)
59         , __pImpl(null)
60 {
61 }
62
63 FloatRectangle::FloatRectangle(const FloatRectangle& rhs)
64         : x(rhs.x)
65         , y(rhs.y)
66         , width(rhs.width)
67         , height(rhs.height)
68         , __pImpl(null)
69 {
70 }
71
72 FloatRectangle::FloatRectangle(float x_, float y_, float width_, float height_)
73         : x(x_)
74         , y(y_)
75         , width(width_)
76         , height(height_)
77         , __pImpl(null)
78 {
79 }
80
81 FloatRectangle::FloatRectangle(const FloatPoint& pt, const FloatDimension& dim)
82         : x(pt.x)
83         , y(pt.y)
84         , width(dim.width)
85         , height(dim.height)
86         , __pImpl(null)
87 {
88 }
89
90 FloatRectangle::~FloatRectangle()
91 {
92 }
93
94 FloatRectangle&
95 FloatRectangle::operator=(const FloatRectangle& rhs)
96 {
97         if (this == &rhs)
98         {
99                 return *this;
100         }
101
102         this->x = rhs.x;
103         this->y = rhs.y;
104         this->width = rhs.width;
105         this->height = rhs.height;
106         this->__pImpl = null;
107
108         return *this;
109 }
110 bool
111 FloatRectangle::operator ==(const FloatRectangle& rhs) const
112 {
113         return ((_IsEqual(this->x, rhs.x) && _IsEqual(this->y, rhs.y) && _IsEqual(this->width, rhs.width) && _IsEqual(this->height, rhs.height)) ? true : false);
114 }
115
116 bool
117 FloatRectangle::operator !=(const FloatRectangle& rhs) const
118 {
119         return (!operator ==(rhs));
120 }
121
122 bool
123 FloatRectangle::Contains(const FloatPoint& point) const
124 {
125         if (IsEmpty())
126         {
127                 return false;
128         }
129
130         if (this->x <= point.x && point.x < (this->x + this->width) &&
131                 this->y <= point.y && point.y < (this->y + this->height))
132         {
133                 return true;
134         }
135
136         return false;
137 }
138
139 bool
140 FloatRectangle::Equals(const Tizen::Base::Object& rhs) const
141 {
142         if (&rhs == null)
143         {
144                 return false;
145         }
146
147         const FloatRectangle* pRect = dynamic_cast <const FloatRectangle*>(&rhs);
148
149         if (pRect == null)
150         {
151                 return false;
152         }
153
154         return (*this == *pRect);
155 }
156
157 int
158 FloatRectangle::GetHashCode(void) const
159 {
160         int intX = int(this->x);
161         int intY = int(this->y);
162         int intWidth = int(this->width);
163         int intHeight = int(this->height);
164
165         return ((intX & 0xFF) << 24) | ((intY & 0xFF) << 16) | ((intWidth & 0xFF) << 8) | (intHeight & 0xFF);
166 }
167
168 FloatRectangle
169 FloatRectangle::GetIntersection(const FloatRectangle& rect) const
170 {
171         FloatRectangle rectResult(0.0f, 0.0f, _EMPTY, _EMPTY);
172
173         if (rect.IsEmpty())
174         {
175                 return rectResult;
176         }
177
178         _Util::Rectangle<float> out;
179         _Util::Rectangle<float> src1;
180         _Util::Rectangle<float> src2;
181
182         src1.x = this->x;
183         src1.y = this->y;
184         src1.w = this->width;
185         src1.h = this->height;
186
187         src2.x = rect.x;
188         src2.y = rect.y;
189         src2.w = rect.width;
190         src2.h = rect.height;
191
192         if (!_Util::IntersectRect(out, src1, src2))
193         {
194                 return rectResult;
195         }
196
197         if (out.w <= 0.0f)
198         {
199                 out.w = _EMPTY;
200         }
201
202         if (out.h <= 0.0f)
203         {
204                 out.h = _EMPTY;
205         }
206
207         // set
208         rectResult.SetBounds(out.x, out.y, out.w, out.h);
209
210         return rectResult;
211 }
212
213 bool
214 FloatRectangle::IsIntersected(const FloatRectangle& rect) const
215 {
216         if (rect.IsEmpty())
217         {
218                 return false;
219         }
220
221         _Util::Rectangle<float> out;
222         _Util::Rectangle<float> src1;
223         _Util::Rectangle<float> src2;
224
225         src1.x = this->x;
226         src1.y = this->y;
227         src1.w = this->width;
228         src1.h = this->height;
229
230         src2.x = rect.x;
231         src2.y = rect.y;
232         src2.w = rect.width;
233         src2.h = rect.height;
234
235         return _Util::IntersectRect(out, src1, src2);
236 }
237
238 FloatRectangle
239 FloatRectangle::GetUnion(const FloatRectangle& rect) const
240 {
241         FloatRectangle rectResult(0.0f, 0.0f, _EMPTY, _EMPTY);
242
243         if (rect.IsEmpty())
244         {
245                 return rectResult;
246         }
247
248         rectResult.x = _Util::Min(this->x, rect.x);
249         rectResult.y = _Util::Min(this->y, rect.y);
250         rectResult.width = _Util::Max((this->x + this->width), (rect.x + rect.width));
251         rectResult.width -= rectResult.x;
252         rectResult.height = _Util::Max((this->y + this->height), (rect.y + rect.height));
253         rectResult.height -= rectResult.y;
254
255         return rectResult;
256 }
257
258 FloatPoint
259 FloatRectangle::GetBottomRight(void) const
260 {
261         return FloatPoint(this->x + this->width, this->y + this->height);
262 }
263
264 FloatPoint
265 FloatRectangle::GetTopLeft(void) const
266 {
267         return FloatPoint(this->x, this->y);
268 }
269
270 void
271 FloatRectangle::SetBounds(float x, float y, float width, float height)
272 {
273         this->x = x;
274         this->y = y;
275         this->width = width;
276         this->height = height;
277 }
278
279 void
280 FloatRectangle::SetBounds(const FloatPoint& pt, const FloatDimension& dim)
281 {
282         this->x = pt.x;
283         this->y = pt.y;
284         this->width = dim.width;
285         this->height = dim.height;
286 }
287
288 bool
289 FloatRectangle::IsEmpty() const
290 {
291         if (this->width <= 0.0f || this->height <= 0.0f)
292         {
293                 return true;
294         }
295
296         return false;
297 }
298
299 void
300 FloatRectangle::SetSize(const FloatDimension& dim)
301 {
302         this->width = dim.width;
303         this->height = dim.height;
304 }
305
306 void
307 FloatRectangle::SetSize(float width, float height)
308 {
309         this->width = width;
310         this->height = height;
311 }
312
313 void
314 FloatRectangle::SetPosition(const FloatPoint& point)
315 {
316         this->x = point.x;
317         this->y = point.y;
318 }
319
320 void
321 FloatRectangle::SetPosition(float x, float y)
322 {
323         this->x = x;
324         this->y = y;
325 }
326
327 void
328 FloatRectangle::Translate(float x, float y)
329 {
330         this->x += x;
331         this->y += y;
332 }
333
334 }} // Tizen::Graphics