1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     util_Rect.h
4   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
5   These coded instructions, statements, and computer programs contain
6   proprietary information of Nintendo of America Inc. and/or Nintendo
7   Company Ltd., and are protected by Federal copyright law. They may
8   not be disclosed to third parties or copied or duplicated in any form,
9   in whole or in part, without the prior written consent of Nintendo.
10   $Rev: 31762 $
11  *---------------------------------------------------------------------------
12 
13 
14 */
15 
16 #ifndef NN_UTIL_UTIL_RECT_H_
17 #define NN_UTIL_UTIL_RECT_H_
18 
19 #ifdef __cplusplus
20 
21 namespace nn { namespace util {
22 
23 //--------------------------------------------------------------------------
24 //
25 //---------------------------------------------------------------------------
26 struct Rect
27 {
28 public:
29     /* ------------------------------------------------------------------------
30             Variables
31        ------------------------------------------------------------------------ */
32     f32 left;     //
33     f32 top;      //
34     f32 right;    //
35     f32 bottom;   //
36 
37     /* ------------------------------------------------------------------------
38             Functions
39        ------------------------------------------------------------------------ */
40     //----------------------------------------
41     //
42     //
43 
44     //--------------------------------------------------------------------------
45     //
46     //---------------------------------------------------------------------------
RectRect47     Rect()
48     : left(0),
49       top(0),
50       right(0),
51       bottom(0)
52     {
53     }
54 
55     //--------------------------------------------------------------------------
56     //
57     //
58     //
59     //
60     //
61     //
62     //---------------------------------------------------------------------------
RectRect63     Rect(f32 l, f32 t, f32 r, f32 b)
64     : left(l),
65       top(t),
66       right(r),
67       bottom(b)
68     {
69     }
70 
71     //--------------------------------------------------------------------------
72     //
73     //---------------------------------------------------------------------------
RectRect74     Rect(const Rect& v)
75     : left(v.left),
76       top(v.top),
77       right(v.right),
78       bottom(v.bottom)
79     {
80     }
81 
82     //--------------------------------------------------------------------------
83     //
84     //---------------------------------------------------------------------------
~RectRect85     ~Rect() {}
86 
87     //
88 
89     //----------------------------------------
90     //
91     //
92 
93     //--------------------------------------------------------------------------
94     //
95     //
96     //
97     //---------------------------------------------------------------------------
GetWidthRect98     f32     GetWidth() const { return right - left; }
99 
100     //--------------------------------------------------------------------------
101     //
102     //
103     //
104     //---------------------------------------------------------------------------
GetHeightRect105     f32     GetHeight() const { return bottom - top; }
106 
107     //--------------------------------------------------------------------------
108     //
109     //
110     //
111     //---------------------------------------------------------------------------
GetXRect112     f32     GetX() const { return left; }
113 
114     //--------------------------------------------------------------------------
115     //
116     //
117     //
118     //---------------------------------------------------------------------------
GetYRect119     f32     GetY() const { return top; }
120 
121     //--------------------------------------------------------------------------
122     //
123     //
124     //
125     //---------------------------------------------------------------------------
SetWidthRect126     void    SetWidth(f32 width) { right = left + width; }
127 
128     //--------------------------------------------------------------------------
129     //
130     //
131     //
132     //---------------------------------------------------------------------------
SetHeightRect133     void    SetHeight(f32 height) { bottom = top + height; }
134 
135     //--------------------------------------------------------------------------
136     //
137     //
138     //
139     //
140     //---------------------------------------------------------------------------
MoveToRect141     void    MoveTo(f32 x, f32 y)
142     {
143         right  = x + GetWidth();
144         left   = x;
145         bottom = y + GetHeight();
146         top    = y;
147     }
148 
149     //--------------------------------------------------------------------------
150     //
151     //
152     //
153     //
154     //---------------------------------------------------------------------------
MoveRect155     void    Move(f32 dx, f32 dy)
156     {
157         left    += dx;
158         right   += dx;
159         top     += dy;
160         bottom  += dy;
161     }
162 
163     //--------------------------------------------------------------------------
164     //
165     //
166     //
167     //
168     //
169     //
170     //---------------------------------------------------------------------------
SetOriginAndSizeRect171     void    SetOriginAndSize(f32 x, f32 y, f32 width, f32 height )
172     {
173         left    = x;
174         right   = x + width;
175         top     = y;
176         bottom  = y + height;
177     }
178 
179     //
180 
181     //--------------------------------------------------------------------------
182     //
183     //---------------------------------------------------------------------------
NormalizeRect184     void    Normalize()
185     {
186         const f32 l = left;
187         const f32 t = top;
188         const f32 r = right;
189         const f32 b = bottom;
190 
191         left    = (r - l) >= 0 ? l : r;
192         right   = (r - l) >= 0 ? r : l;
193         top     = (b - t) >= 0 ? t : b;
194         bottom  = (b - t) >= 0 ? b : t;
195     }
196 };
197 
198 }} /* namespace nn::util */
199 
200 #endif // __cplusplus
201 
202 #endif //  NN_UTIL_UTIL_RECT_H_
203