1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     ut_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   $Revision:$
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_UT_RECT_H_
17 #define NW_UT_RECT_H_
18 
19 #include <nw/types.h>
20 //#include <nw/math/arithmetic.h>
21 
22 namespace nw {
23 namespace ut {
24 
25 
26 //--------------------------------------------------------------------------
27 //! @brief        矩形を表すクラスです。
28 //---------------------------------------------------------------------------
29 struct Rect
30 {
31 public:
32     /* ------------------------------------------------------------------------
33             変数
34        ------------------------------------------------------------------------ */
35     f32 left;     //!< 矩形の左座標です。
36     f32 top;      //!< 矩形の上座標です。
37     f32 right;    //!< 矩形の右座標です。
38     f32 bottom;   //!< 矩形の下座標です。
39 
40 
41     /* ------------------------------------------------------------------------
42             関数
43        ------------------------------------------------------------------------ */
44     //----------------------------------------
45     //! @name コンストラクタ/デストラクタ
46     //@{
47 
48     //--------------------------------------------------------------------------
49     //! @brief        デフォルトコンストラクタです。
50     //---------------------------------------------------------------------------
RectRect51     Rect()
52     : left(0),
53       top(0),
54       right(0),
55       bottom(0)
56     {
57     }
58 
59     //--------------------------------------------------------------------------
60     //! @brief        コンストラクタです。
61     //!
62     //! @param[in]    l       矩形の左座標です。
63     //! @param[in]    t       矩形の上座標です。
64     //! @param[in]    r       矩形の右座標です。
65     //! @param[in]    b       矩形の下座標です。
66     //---------------------------------------------------------------------------
RectRect67     Rect(f32 l, f32 t, f32 r, f32 b)
68     : left(l),
69       top(t),
70       right(r),
71       bottom(b)
72     {
73     }
74 
75     //--------------------------------------------------------------------------
76     //! @brief        コピーコンストラクタです。
77     //---------------------------------------------------------------------------
RectRect78     Rect(const Rect& v)
79     : left(v.left),
80       top(v.top),
81       right(v.right),
82       bottom(v.bottom)
83     {
84     }
85 
86     //--------------------------------------------------------------------------
87     //! @brief        デストラクタです。
88     //---------------------------------------------------------------------------
~RectRect89     ~Rect() {}
90 
91     //@}
92 
93     //----------------------------------------
94     //! @name 設定/取得
95     //@{
96 
97     //--------------------------------------------------------------------------
98     //! @brief        矩形の幅を取得します。
99     //!
100     //! @return       矩形の幅です。
101     //---------------------------------------------------------------------------
GetWidthRect102     f32     GetWidth() const { return right - left; }
103 
104     //--------------------------------------------------------------------------
105     //! @brief        矩形の高さを取得します。
106     //!
107     //! @return       矩形の高さです。
108     //---------------------------------------------------------------------------
GetHeightRect109     f32     GetHeight() const { return bottom - top; }
110 
111     //--------------------------------------------------------------------------
112     //! @brief        矩形の基点となるX座標を取得します。
113     //!
114     //! @return       矩形の基点となるX座標です。
115     //---------------------------------------------------------------------------
GetXRect116     f32     GetX() const { return left; }
117 
118     //--------------------------------------------------------------------------
119     //! @brief        矩形の基点となるY座標を取得します。
120     //!
121     //! @return       矩形の基点となるY座標です。
122     //---------------------------------------------------------------------------
GetYRect123     f32     GetY() const { return top; }
124 
125     //--------------------------------------------------------------------------
126     //! @brief        矩形の幅を設定します。
127     //!
128     //! @param[in]    width   矩形の幅です。
129     //---------------------------------------------------------------------------
SetWidthRect130     void    SetWidth(f32 width) { right = left + width; }
131 
132     //--------------------------------------------------------------------------
133     //! @brief        矩形の高さを設定します。
134     //!
135     //! @param[in]    height  矩形の高さです。
136     //---------------------------------------------------------------------------
SetHeightRect137     void    SetHeight(f32 height) { bottom = top + height; }
138 
139     //--------------------------------------------------------------------------
140     //! @brief        矩形の左上座標を移動します。
141     //!
142     //! @param[in]    x       矩形の左上座標の x 座標です。
143     //! @param[in]    y       矩形の左上座標の y 座標です。
144     //---------------------------------------------------------------------------
MoveToRect145     void    MoveTo(f32 x, f32 y)
146     {
147         right  = x + GetWidth();
148         left   = x;
149         bottom = y + GetHeight();
150         top    = y;
151     }
152 
153     //--------------------------------------------------------------------------
154     //! @brief        矩形を並行移動します。
155     //!
156     //! @param[in]    dx       x 方向へ矩形を移動する移動量です。
157     //! @param[in]    dy       y 方向へ矩形を移動する移動量です。
158     //---------------------------------------------------------------------------
MoveRect159     void    Move(f32 dx, f32 dy)
160     {
161         left    += dx;
162         right   += dx;
163         top     += dy;
164         bottom  += dy;
165     }
166 
167     //--------------------------------------------------------------------------
168     //! @brief        原点とサイズを設定します。
169     //!
170     //! @param[in]    x       矩形の基点となるY座標です。
171     //! @param[in]    y       矩形の基点となるX座標です。
172     //! @param[in]    width   矩形の幅です。
173     //! @param[in]    height  矩形の高さです。
174     //---------------------------------------------------------------------------
SetOriginAndSizeRect175     void    SetOriginAndSize(f32 x, f32 y, f32 width, f32 height )
176     {
177         left    = x;
178         right   = x + width;
179         top     = y;
180         bottom  = y + height;
181     }
182 
183     //@}
184 
185     //--------------------------------------------------------------------------
186     //! @brief        矩形情報を正常な値に正規化します。
187     //---------------------------------------------------------------------------
NormalizeRect188     void    Normalize()
189     {
190         const f32 l = left;
191         const f32 t = top;
192         const f32 r = right;
193         const f32 b = bottom;
194 
195         left    = (r - l) >= 0 ? l : r;
196         right   = (r - l) >= 0 ? r : l;
197         top     = (b - t) >= 0 ? t : b;
198         bottom  = (b - t) >= 0 ? b : t;
199     }
200 };
201 
202 
203 
204 } /* namespace ut */
205 } /* namespace nw */
206 
207 #endif //  NW_UT_RECT_H_
208