RECT.C
[大目次 | 目次 | 関数]
1|/***********************************************************************
2| 1. <<< 矩形 (Rect) >>>
3|************************************************************************/
4|
5|#include "mixer_precomp.h" /* Auto precompiled header, Look at mixer-... folder */
6|// #pragma hdrstop
7|
8|#if defined(USES_MXP_AUTOINC)
9| #include "rect.ah" /* Auto include header, Look at mixer-... folder */
10|#endif
11|
12|/*-------------------------------------------------------------------------*/
13|/* 2. <<<< ◆(Rect) 矩形(幅・高さ指定) >>>> */
14|/*-------------------------------------------------------------------------*/
15|
16|/***********************************************************************
17| 2-1. <<< [Rect_init] 初期化する >>>
18|************************************************************************/
19|void Rect_init( Rect* m, int x, int y, int w, int h )
20|{
21| m->x = x; m->y = y;
22| m->w = w; m->h = h;
23|}
24|
25|
26|
27|/***********************************************************************
28| 2-2. <<< [Rect_toAnd] 共通部分の矩形にする >>>
29|【機能】
30|・矩形 a と矩形 b の共通部分を ans とする。
31|************************************************************************/
32|void Rect_toAnd( Rect* ans, Rect* a, Rect* b )
33|{
34| #define MAX(x,y) ((x)<(y) ? (y) : (x) )
35| #define MIN(x,y) ((x)<(y) ? (x) : (y) )
36|
37| ans->x = MAX( a->x, b->x );
38| ans->y = MAX( a->y, b->y );
39|
40| ans->w = MIN( a->x + a->w, b->x + b->w ) - ans->x;
41| ans->h = MIN( a->y + a->h, b->y + b->h ) - ans->y;
42|
43| #undef MAX
44| #undef MIN
45|}
46|
47|
48|
49|/***********************************************************************
50| 2-3. <<< [Rect_print] デバッグ表示する >>>
51|【引数】
52| ・char* title; 行頭に加えるメッセージ
53|************************************************************************/
54|#ifdef USES_ERRORS
55|#ifndef ERRORS_CUT_DEBUG_TOOL
56|void Rect_print( Rect* m, const char* title )
57|{
58| Errors_printf( "%s:Rect(%p): (x,y)-(w,h) = (%d, %d) - (%d, %d)",
59| title, m, m->x, m->y, m->w, m->h );
60|}
61|#endif /* not ERRORS_CUT_DEBUG_TOOL */
62|#endif
63|
64|
65|
66|/***********************************************************************
67| 2-4. <<< [Rect_isPointIn] 矩形の内部に点があるかどうかを返す(端も含む) >>>
68|【引数】
69| ・Rect* rect; 矩形
70| ・int x,y; 点
71| ・bool 返り値; 矩形の内部に点があるかどうか(端も含む)
72|************************************************************************/
73|bool Rect_isPointIn( Rect* rect, int x, int y )
74|{
75| const int x1 = rect->x;
76| const int y1 = rect->y;
77| const int x2 = rect->x + rect->w - 1;
78| const int y2 = rect->y + rect->h - 1;
79|
80| return ( x1 <= x && x <= x2 && y1 <= y && y <= y2 );
81|}
82|
83|
84|
85|/***********************************************************************
86| 2-5. <<< [Rect_getPointPos] 矩形と点の位置関係を返す >>>
87|【引数】
88| ・Rect* rect; 矩形
89| ・int x,y; 点
90| ・int 返り値; RECT_X_LT 等の、X, Y に関する論理和
91|************************************************************************/
92|int Rect_getPointPos( Rect* rect, int x, int y )
93|{
94| int f;
95|
96| if ( x < rect->x ) f = RECT_X_LT;
97| else if ( x == rect->x ) f = RECT_X_LE;
98| else if ( x < rect->x + rect->w ) f = RECT_X_E;
99| else if ( x == rect->x + rect->w ) f = RECT_X_GE;
100| else f = RECT_X_GT;
101|
102| if ( y < rect->y ) f |= RECT_Y_LT;
103| else if ( y == rect->y ) f |= RECT_Y_LE;
104| else if ( y < rect->y + rect->h ) f |= RECT_Y_E;
105| else if ( y == rect->y + rect->h ) f |= RECT_Y_GE;
106| else f |= RECT_Y_GT;
107|
108| return f;
109|}
110|
111|
112|
113|/***********************************************************************
114| 2-6. <<< [Rect_getHitHandleNum] CadPrim::GetHitHandleNum の実装部 >>>
115|【補足】
116|・ハンドル番号は、左上が1、上が2、右上が3、左が4、右が5、左下が6、
117| 下が7、右下が8です。
118|・ハンドルの数は、Rect_nHandle です。
119|************************************************************************/
120|int Rect_getHitHandleNum( Rect* m, int x, int y, int* dx, int* dy )
121|{
122| int dx1, dy1, dx2, dy2, dx3, dy3;
123| enum { diff = 8 };
124|
125| dx1 = m->x - x;
126| dy1 = m->y - y;
127| dx2 = m->x + (m->w - 1) / 2 - x;
128| dy2 = m->y + (m->h - 1) / 2 - y;
129| dx3 = m->x + m->w - 1 - x;
130| dy3 = m->y + m->h - 1 - y;
131|
132| *dx = 0; *dy = 0;
133|
134| if ( dx3 >= -diff && dx3 <= diff && dy3 >= -diff && dy3 <= diff )
135| return 8;
136| if ( dx2 >= -diff && dx2 <= diff && dy3 >= -diff && dy3 <= diff )
137| return 7;
138| if ( dx3 >= -diff && dx3 <= diff && dy2 >= -diff && dy2 <= diff )
139| return 5;
140| else if ( dx1 >= -diff && dx1 <= diff && dy1 >= -diff && dy1 <= diff )
141| return 1;
142| else if ( dx2 >= -diff && dx2 <= diff && dy1 >= -diff && dy1 <= diff )
143| return 2;
144| else if ( dx3 >= -diff && dx3 <= diff && dy1 >= -diff && dy1 <= diff )
145| return 3;
146| else if ( dx1 >= -diff && dx1 <= diff && dy2 >= -diff && dy2 <= diff )
147| return 4;
148| else if ( dx1 >= -diff && dx1 <= diff && dy3 >= -diff && dy3 <= diff )
149| return 6;
150| else if ( m->x - diff <= x && x <= m->x + m->w + diff &&
151| m->y - diff <= y && y <= m->y + m->h + diff ) {
152| *dx = m->x - x; *dy = m->y - y;
153| return -1;
154| }
155| else
156| return 0;
157|}
158|
159|
160|/***********************************************************************
161| 2-7. <<< [Rect_moveByHandle] CadPrim::MoveByHandle の実装部 >>>
162|************************************************************************/
163|void Rect_moveByHandle( Rect* m, int iHandle, int x, int y )
164|{
165| int dx, dy;
166|
167| switch ( iHandle ) {
168| case 1:
169| dx = x - m->x; dy = y - m->y;
170| m->x = x; m->y = y;
171| m->w -= dx; m->h -= dy;
172| break;
173|
174| case 2:
175| dy = y - m->y;
176| m->y = y;
177| m->h -= dy;
178| break;
179|
180| case 3:
181| dy = y - m->y;
182| m->y = y;
183| m->w = x - m->x + 1; m->h -= dy;
184| break;
185|
186| case 4:
187| dx = x - m->x;
188| m->x = x;
189| m->w -= dx;
190| break;
191|
192| case 5:
193| m->w = x - m->x + 1;
194| break;
195|
196| case 6:
197| dx = x - m->x;
198| m->x = x;
199| m->w -= dx; m->h = y - m->y + 1;
200|
201| break;
202|
203| case 7:
204| m->h = y - m->y + 1;
205| break;
206|
207| case 8:
208| m->w = x - m->x + 1; m->h = y - m->y + 1;
209| break;
210|
211| case -1:
212| m->x = x; m->y = y;
213| break;
214| }
215|}
216|
217|
218|/***********************************************************************
219| 2-8. <<< [Rect_getCenterOfHandle] ハンドルの中心座標を取得する >>>
220|【引数】
221| ・Rect* rect; 矩形
222| ・int iHandle; ハンドル番号(1〜Rect_nHandle)
223| ・int* x,y; 座標を格納する領域のアドレス
224|【補足】
225|・ハンドルについては、Rect_getHitHandleNum を参照。
226|************************************************************************/
227|void Rect_getCenterOfHandle( Rect* m, int iHandle, int* x, int* y )
228|{
229| switch ( iHandle ) {
230| case 1:
231| *x = m->x; *y = m->y;
232| break;
233|
234| case 2:
235| *x = m->x + (m->w - 1) / 2; *y = m->y;
236| break;
237|
238| case 3:
239| *x = m->x + m->w - 1; *y = m->y;
240| break;
241|
242| case 4:
243| *x = m->x; *y = m->y + (m->h - 1) / 2;
244| break;
245|
246| case 5:
247| *x = m->x + m->w - 1; *y = m->y + (m->h - 1) / 2;
248| break;
249|
250| case 6:
251| *x = m->x; *y = m->y + m->h - 1;
252| break;
253|
254| case 7:
255| *x = m->x + (m->w - 1) / 2; *y = m->y + m->h - 1;
256| break;
257|
258| case 8:
259| *x = m->x + m->w - 1; *y = m->y + m->h - 1;
260| break;
261| }
262|}
263|
264|
265|/*-------------------------------------------------------------------------*/
266|/* 3. <<<< ◆ (Rect_2XY) 矩形(2点指定)>>>> */
267|/*-------------------------------------------------------------------------*/
268|
269|/***********************************************************************
270| 3-1. <<< [Rect_2XY_setInDesktop] ウィンドウが外に出ないようにデスクトップの中に入れる >>>
271|【引数】
272| ・Rect_2XY* wnd; ウィンドウの位置(入出力)
273| ・Rect_2XY* desktop; デスクトップのサイズ
274|【補足】
275|・デスクトップの大きさは、GetDesktopWindow と GetWindowRect で取得できます。
276|・ウィンドウの移動は、MoveWindow です。
277|************************************************************************/
278|void Rect_2XY_setInDesktop( Rect_2XY* wnd, Rect_2XY* desktop )
279|{
280| if ( wnd->x2 > desktop->x2 ) {
281| wnd->x1 -= wnd->x2 - desktop->x2;
282| wnd->x2 = desktop->x2;
283| if ( wnd->x1 < 0 ) wnd->x1 = 0;
284| }
285| if ( wnd->y2 > desktop->y2 ) {
286| wnd->y1 -= wnd->y2 - desktop->y2;
287| wnd->y2 = desktop->y2;
288| if ( wnd->y1 < 0 ) wnd->y1 = 0;
289| }
290|}
291|
292|