xref: /haiku/headers/private/interface/clipping.h (revision 5c9e6a3953923a7c60c3d9c69c4f935492f26997)
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 
46 static inline void
47 offset_rect(clipping_rect &rect, int32 x, int32 y)
48 {
49 	rect.left += x;
50 	rect.top += y;
51 	rect.right += x;
52 	rect.bottom += y;
53 }
54 
55 
56 static inline BRect
57 to_BRect(clipping_rect rect)
58 {
59 	return BRect(rect.left, rect.top, rect.right, rect.bottom);
60 }
61 
62 
63 static inline clipping_rect
64 to_clipping_rect(BRect rect)
65 {
66 	clipping_rect clipRect;
67 
68 	clipRect.left = (int32)floor(rect.left);
69 	clipRect.top = (int32)floor(rect.top);
70 	clipRect.right = (int32)ceil(rect.right);
71 	clipRect.bottom = (int32)ceil(rect.bottom);
72 
73 	return clipRect;
74 }
75 
76 
77 static inline bool
78 point_in(clipping_rect rect, int32 px, int32 py)
79 {
80 	if (px >= rect.left && px <= rect.right
81 			&& py >= rect.top && py <= rect.bottom)
82 		return true;
83 	return false;
84 }
85 
86 
87 static inline bool
88 point_in(clipping_rect rect, BPoint pt)
89 {
90 	if (pt.x >= rect.left && pt.x <= rect.right
91 			&& pt.y >= rect.top && pt.y <= rect.bottom)
92 		return true;
93 	return false;
94 }
95 
96 
97 static inline bool
98 valid_rect(clipping_rect rect)
99 {
100 	if (rect.left <= rect.right && rect.top <= rect.bottom)
101 		return true;
102 	return false;
103 }
104 
105 
106 static inline bool
107 rects_intersect(clipping_rect rectA, clipping_rect rectB)
108 {
109 	return !(rectA.left > rectB.right || rectA.top > rectB.bottom
110 			|| rectA.right < rectB.left || rectA.bottom < rectB.top);
111 }
112 
113 
114 static inline int32
115 rect_width(clipping_rect rect)
116 {
117 	return rect.right - rect.left;
118 }
119 
120 
121 static inline int32
122 rect_height(clipping_rect rect)
123 {
124 	return rect.bottom - rect.top;
125 }
126 
127 #endif // __CLIPPING_H
128