xref: /haiku/headers/private/interface/clipping.h (revision 51978af14a173e7fae0563b562be5603bc652aeb)
1 #ifndef __CLIPPING_H
2 #define __CLIPPING_H
3 
4 #include <Region.h>
5 #include <SupportDefs.h>
6 
7 
8 /*	Some methods to manipulate clipping_rects.
9 	basically you can do almost everything you do with
10 	BRects, just that clipping_rects can only have integer
11 	coordinates (a thing that makes these perfect for drawing
12 	calculations).
13 */
14 
15 static inline clipping_rect
16 union_rect(clipping_rect r1, clipping_rect r2)
17 {
18 	clipping_rect rect;
19 
20 	rect.left = min_c(r1.left, r2.left);
21 	rect.top = min_c(r1.top, r2.top);
22 	rect.right = max_c(r1.right, r2.right);
23 	rect.bottom = max_c(r1.bottom, r2.bottom);
24 
25 	return rect;
26 }
27 
28 
29 // Returns the intersection of the given rects.
30 // The caller should check if the returned rect is valid. If it isn't valid,
31 // then the two rectangles don't intersect.
32 static inline clipping_rect
33 sect_rect(clipping_rect r1, clipping_rect r2)
34 {
35 	clipping_rect rect;
36 
37 	rect.left = max_c(r1.left, r2.left);
38 	rect.top = max_c(r1.top, r2.top);
39 	rect.right = min_c(r1.right, r2.right);
40 	rect.bottom = min_c(r1.bottom, r2.bottom);
41 
42 	return rect;
43 }
44 
45 static inline void
46 offset_rect(clipping_rect &rect, int32 x, int32 y)
47 {
48 	rect.left += x;
49 	rect.top += y;
50 	rect.right += x;
51 	rect.bottom += y;
52 }
53 
54 
55 static inline BRect
56 to_BRect(clipping_rect rect)
57 {
58 	return BRect(rect.left, rect.top, rect.right, rect.bottom);
59 }
60 
61 
62 static inline clipping_rect
63 to_clipping_rect(BRect rect)
64 {
65 	clipping_rect clipRect;
66 
67 	clipRect.left = (int32)floor(rect.left);
68 	clipRect.top = (int32)floor(rect.top);
69 	clipRect.right = (int32)ceil(rect.right);
70 	clipRect.bottom = (int32)ceil(rect.bottom);
71 
72 	return clipRect;
73 }
74 
75 
76 static inline bool
77 point_in(clipping_rect rect, int32 px, int32 py)
78 {
79 	if (px >= rect.left && px <= rect.right
80 			&& py >= rect.top && py <= rect.bottom)
81 		return true;
82 	return false;
83 }
84 
85 
86 static inline bool
87 point_in(clipping_rect rect, BPoint pt)
88 {
89 	if (pt.x >= rect.left && pt.x <= rect.right
90 			&& pt.y >= rect.top && pt.y <= rect.bottom)
91 		return true;
92 	return false;
93 }
94 
95 
96 static inline bool
97 valid_rect(clipping_rect rect)
98 {
99 	if (rect.left <= rect.right && rect.top <= rect.bottom)
100 		return true;
101 	return false;
102 }
103 
104 
105 static inline bool
106 rects_intersect(clipping_rect rectA, clipping_rect rectB)
107 {
108 	return valid_rect(sect_rect(rectA, rectB));
109 }
110 
111 
112 static inline int32
113 rect_width(clipping_rect rect)
114 {
115 	return rect.right - rect.left;
116 }
117 
118 
119 static inline int32
120 rect_height(clipping_rect rect)
121 {
122 	return rect.bottom - rect.top;
123 }
124 
125 #endif // __CLIPPING_H
126