1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS Console Server DLL
4  * FILE:            win32ss/user/winsrv/consrv/include/rect.h
5  * PURPOSE:         Rectangle helper functions
6  * PROGRAMMERS:     G� van Geldorp
7  *                  Jeffrey Morlan
8  */
9 
10 #pragma once
11 
12 #define ConioInitLongRect(Rect, Top, Left, Bottom, Right) \
13 do {    \
14     ((Rect)->top)    = Top;     \
15     ((Rect)->left)   = Left;    \
16     ((Rect)->bottom) = Bottom;  \
17     ((Rect)->right)  = Right;   \
18 } while (0)
19 
20 #define ConioInitRect(Rect, top, left, bottom, right) \
21 do {    \
22     ((Rect)->Top)    = top;     \
23     ((Rect)->Left)   = left;    \
24     ((Rect)->Bottom) = bottom;  \
25     ((Rect)->Right)  = right;   \
26 } while (0)
27 
28 #define ConioIsRectEmpty(Rect) \
29     (((Rect)->Left > (Rect)->Right) || ((Rect)->Top > (Rect)->Bottom))
30 
31 #define ConioRectHeight(Rect) \
32     (((Rect)->Top > (Rect)->Bottom) ? 0 : ((Rect)->Bottom - (Rect)->Top + 1))
33 #define ConioRectWidth(Rect) \
34     (((Rect)->Left > (Rect)->Right) ? 0 : ((Rect)->Right - (Rect)->Left + 1))
35 
36 
37 static __inline BOOLEAN
38 ConioGetIntersection(OUT PSMALL_RECT Intersection,
39                      IN PSMALL_RECT Rect1,
40                      IN PSMALL_RECT Rect2)
41 {
42     if ( ConioIsRectEmpty(Rect1) ||
43          ConioIsRectEmpty(Rect2) ||
44         (Rect1->Top  > Rect2->Bottom) ||
45         (Rect1->Left > Rect2->Right)  ||
46         (Rect1->Bottom < Rect2->Top)  ||
47         (Rect1->Right  < Rect2->Left) )
48     {
49         /* The rectangles do not intersect */
50         ConioInitRect(Intersection, 0, -1, 0, -1);
51         return FALSE;
52     }
53 
54     ConioInitRect(Intersection,
55                   max(Rect1->Top   , Rect2->Top   ),
56                   max(Rect1->Left  , Rect2->Left  ),
57                   min(Rect1->Bottom, Rect2->Bottom),
58                   min(Rect1->Right , Rect2->Right ));
59 
60     return TRUE;
61 }
62 
63 static __inline BOOLEAN
64 ConioGetUnion(OUT PSMALL_RECT Union,
65               IN PSMALL_RECT Rect1,
66               IN PSMALL_RECT Rect2)
67 {
68     if (ConioIsRectEmpty(Rect1))
69     {
70         if (ConioIsRectEmpty(Rect2))
71         {
72             ConioInitRect(Union, 0, -1, 0, -1);
73             return FALSE;
74         }
75         else
76         {
77             *Union = *Rect2;
78         }
79     }
80     else if (ConioIsRectEmpty(Rect2))
81     {
82         *Union = *Rect1;
83     }
84     else
85     {
86         ConioInitRect(Union,
87                       min(Rect1->Top   , Rect2->Top   ),
88                       min(Rect1->Left  , Rect2->Left  ),
89                       max(Rect1->Bottom, Rect2->Bottom),
90                       max(Rect1->Right , Rect2->Right ));
91     }
92 
93     return TRUE;
94 }
95