1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     util_Rect.h
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 25398 $
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 //! @brief        矩形を表すクラスです。
25 //---------------------------------------------------------------------------
26 struct Rect
27 {
28 public:
29     /* ------------------------------------------------------------------------
30             変数
31        ------------------------------------------------------------------------ */
32     f32 left;     //!< 矩形の左座標です。
33     f32 top;      //!< 矩形の上座標です。
34     f32 right;    //!< 矩形の右座標です。
35     f32 bottom;   //!< 矩形の下座標です。
36 
37     /* ------------------------------------------------------------------------
38             関数
39        ------------------------------------------------------------------------ */
40     //----------------------------------------
41     //! @name コンストラクタ/デストラクタ
42     //@{
43 
44     //--------------------------------------------------------------------------
45     //! @brief        デフォルトコンストラクタです。
46     //---------------------------------------------------------------------------
RectRect47     Rect()
48     : left(0),
49       top(0),
50       right(0),
51       bottom(0)
52     {
53     }
54 
55     //--------------------------------------------------------------------------
56     //! @brief        コンストラクタです。
57     //!
58     //! @param[in]    l       矩形の左座標です。
59     //! @param[in]    t       矩形の上座標です。
60     //! @param[in]    r       矩形の右座標です。
61     //! @param[in]    b       矩形の下座標です。
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     //! @brief        コピーコンストラクタです。
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     //! @brief        デストラクタです。
84     //---------------------------------------------------------------------------
~RectRect85     ~Rect() {}
86 
87     //@}
88 
89     //----------------------------------------
90     //! @name 値の設定/取得
91     //@{
92 
93     //--------------------------------------------------------------------------
94     //! @brief        矩形の幅を取得します。
95     //!
96     //! @return       矩形の幅です。
97     //---------------------------------------------------------------------------
GetWidthRect98     f32     GetWidth() const { return right - left; }
99 
100     //--------------------------------------------------------------------------
101     //! @brief        矩形の高さを取得します。
102     //!
103     //! @return       矩形の高さです。
104     //---------------------------------------------------------------------------
GetHeightRect105     f32     GetHeight() const { return bottom - top; }
106 
107     //--------------------------------------------------------------------------
108     //! @brief        矩形の基点となるX座標を取得します。
109     //!
110     //! @return       矩形の基点となるX座標です。
111     //---------------------------------------------------------------------------
GetXRect112     f32     GetX() const { return left; }
113 
114     //--------------------------------------------------------------------------
115     //! @brief        矩形の基点となるY座標を取得します。
116     //!
117     //! @return       矩形の基点となるY座標です。
118     //---------------------------------------------------------------------------
GetYRect119     f32     GetY() const { return top; }
120 
121     //--------------------------------------------------------------------------
122     //! @brief        矩形の幅を設定します。
123     //!
124     //! @param[in]    width   矩形の幅です。
125     //---------------------------------------------------------------------------
SetWidthRect126     void    SetWidth(f32 width) { right = left + width; }
127 
128     //--------------------------------------------------------------------------
129     //! @brief        矩形の高さを設定します。
130     //!
131     //! @param[in]    height  矩形の高さです。
132     //---------------------------------------------------------------------------
SetHeightRect133     void    SetHeight(f32 height) { bottom = top + height; }
134 
135     //--------------------------------------------------------------------------
136     //! @brief        矩形の左上座標を移動します。
137     //!
138     //! @param[in]    x       矩形の左上座標の x 座標です。
139     //! @param[in]    y       矩形の左上座標の y 座標です。
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     //! @brief        矩形を並行移動します。
151     //!
152     //! @param[in]    dx       x 方向へ矩形を移動する移動量です。
153     //! @param[in]    dy       y 方向へ矩形を移動する移動量です。
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     //! @brief        原点とサイズを設定します。
165     //!
166     //! @param[in]    x       矩形の基点となるY座標です。
167     //! @param[in]    y       矩形の基点となるX座標です。
168     //! @param[in]    width   矩形の幅です。
169     //! @param[in]    height  矩形の高さです。
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     //! @brief        矩形情報を正常な値に正規化します。
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