EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / include / eina_inline_rectangle.x
1 /* EINA - EFL data type library
2  * Copyright (C) 2007-2008 Jorge Luis Zapata Muga
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library;
16  * if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef EINA_INLINE_RECTANGLE_H__
20 #define EINA_INLINE_RECTANGLE_H__
21
22 /**
23  * @addtogroup Eina_Rectangle_Group Rectangle
24  *
25  * @brief These functions provide rectangle management.
26  *
27  * @{
28  */
29
30 /**
31  * @brief Check if the given spans intersect.
32  *
33  * @param c1 The column of the first span.
34  * @param l1 The length of the first span.
35  * @param c2 The column of the second span.
36  * @param l2 The length of the second span.
37  * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
38  *
39  * This function returns #EINA_TRUE if the  given spans intersect, #EINA_FALSE
40  * otherwise.
41  */
42 static inline int
43 eina_spans_intersect(int c1, int l1, int c2, int l2)
44 {
45         return (!(((c2 + l2) <= c1) || (c2 >= (c1 + l1))));
46 }
47
48 /**
49  * @brief Check if the given rectangle is empty.
50  *
51  * @param r The rectangle to check.
52  * @return #EINA_TRUE if the rectangle is empty, #EINA_FALSE otherwise.
53  *
54  * This function returns #EINA_TRUE if @p r is empty, #EINA_FALSE
55  * otherwise. No check is done on @p r, so it must be a valid
56  * rectangle.
57  */
58 static inline Eina_Bool
59 eina_rectangle_is_empty(const Eina_Rectangle *r)
60 {
61         return ((r->w < 1) || (r->h < 1)) ? EINA_TRUE : EINA_FALSE;
62 }
63
64 /**
65  * @brief Set the coordinates and size of the given rectangle.
66  *
67  * @param r The rectangle.
68  * @param x The top-left x coordinate of the rectangle.
69  * @param y The top-left y coordinate of the rectangle.
70  * @param w The width of the rectangle.
71  * @param h The height of the rectangle.
72  *
73  * This function sets its top-left x coordinate to @p x, its top-left
74  * y coordinate to @p y, its width to @p w and its height to @p h. No
75  * check is done on @p r, so it must be a valid rectangle.
76  */
77 static inline void
78 eina_rectangle_coords_from(Eina_Rectangle *r, int x, int y, int w, int h)
79 {
80         r->x = x;
81         r->y = y;
82         r->w = w;
83         r->h = h;
84 }
85
86 /**
87  * @brief Check if the given rectangles intersect.
88  *
89  * @param r1 The first rectangle.
90  * @param r2 The second rectangle.
91  * @return #EINA_TRUE if the rectangles intersect, #EINA_FALSE otherwise.
92  *
93  * This function returns #EINA_TRUE if @p r1 and @p r2 intersect, #EINA_FALSE
94  * otherwise. No check is done on @p r1 and @p r2, so they must be valid
95  * rectangles.
96  */
97 static inline Eina_Bool
98 eina_rectangles_intersect(const Eina_Rectangle *r1, const Eina_Rectangle *r2)
99 {
100         return (eina_spans_intersect(r1->x, r1->w, r2->x, r2->w) && eina_spans_intersect(r1->y, r1->h, r2->y, r2->h)) ? EINA_TRUE : EINA_FALSE;
101 }
102
103 /**
104  * @brief Check if the given x-coordinate is in the rectangle .
105  *
106  * @param r The rectangle.
107  * @param x The x coordinate.
108  * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
109  *
110  * This function returns #EINA_TRUE if @p x is in @p r with respect to
111  * the horizontal direction, #EINA_FALSE otherwise. No check is done
112  * on @p r, so it must be a valid rectangle.
113  */
114 static inline Eina_Bool
115 eina_rectangle_xcoord_inside(const Eina_Rectangle *r, int x)
116 {
117         return ((x >= r->x) && (x < (r->x + r->w))) ? EINA_TRUE : EINA_FALSE;
118 }
119
120 /**
121  * @brief Check if the given y-coordinate is in the rectangle .
122  *
123  * @param r The rectangle.
124  * @param y The y coordinate.
125  * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
126  *
127  * This function returns #EINA_TRUE if @p y is in @p r with respect to
128  * the vertical direction, #EINA_FALSE otherwise. No check is done
129  * on @p r, so it must be a valid rectangle.
130  */
131 static inline Eina_Bool
132 eina_rectangle_ycoord_inside(const Eina_Rectangle *r, int y)
133 {
134         return ((y >= r->y) && (y < (r->y + r->h))) ? EINA_TRUE : EINA_FALSE;
135 }
136
137 /**
138  * @brief Check if the given point is in the rectangle .
139  *
140  * @param r The rectangle.
141  * @param x The x coordinate of the point.
142  * @param y The y coordinate of the point.
143  * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
144  *
145  * This function returns #EINA_TRUE if the point of coordinate (@p x,
146  * @p y) is in @p r, #EINA_FALSE otherwise. No check is done on @p r,
147  * so it must be a valid rectangle.
148  */
149 static inline Eina_Bool
150 eina_rectangle_coords_inside(const Eina_Rectangle *r, int x, int y)
151 {
152         return (eina_rectangle_xcoord_inside(r, x) && eina_rectangle_ycoord_inside(r, y)) ? EINA_TRUE : EINA_FALSE;
153 }
154
155 /**
156  * @brief Get the union of two rectangles.
157  *
158  * @param dst The first rectangle.
159  * @param src The second rectangle.
160  *
161  * This function get the union of the rectangles @p dst and @p src. The
162  * result is stored in @p dst. No check is done on @p dst or @p src,
163  * so they must be valid rectangles.
164  */
165 static inline void
166 eina_rectangle_union(Eina_Rectangle *dst, const Eina_Rectangle *src)
167 {
168         /* left */
169         if (dst->x > src->x)
170         {
171                 dst->w += dst->x - src->x;
172                 dst->x = src->x;
173         }
174         /* right */
175         if ((dst->x + dst->w) < (src->x + src->w))
176                 dst->w = src->x + src->w;
177         /* top */
178         if (dst->y > src->y)
179         {
180                 dst->h += dst->y - src->y;
181                 dst->y = src->y;
182         }
183         /* bottom */
184         if ((dst->y + dst->h) < (src->y + src->h))
185                 dst->h = src->y + src->h;
186 }
187
188 /**
189  * @brief Get the intersection of two rectangles.
190  *
191  * @param dst The first rectangle.
192  * @param src The second rectangle.
193  * @return #EINA_TRUE if the rectangles intersect, #EINA_FALSE
194  * otherwise.
195  *
196  * This function get the intersection of the rectangles @p dst and
197  * @p src. The result is stored in @p dst. No check is done on @p dst
198  * or @p src, so they must be valid rectangles.
199  */
200 static inline Eina_Bool
201 eina_rectangle_intersection(Eina_Rectangle *dst, const Eina_Rectangle *src)
202 {
203         if (!(eina_rectangles_intersect(dst, src)))
204                 return EINA_FALSE;
205
206         /* left */
207         if (dst->x < src->x)
208         {
209                 dst->w += dst->x - src->x;
210                 dst->x = src->x;
211                 if (dst->w < 0)
212                         dst->w = 0;
213         }
214         /* right */
215         if ((dst->x + dst->w) > (src->x + src->w))
216                 dst->w = src->x + src->w - dst->x;
217         /* top */
218         if (dst->y < src->y)
219         {
220                 dst->h += dst->y - src->y;
221                 dst->y = src->y;
222                 if (dst->h < 0)
223                         dst->h = 0;
224         }
225         /* bottom */
226         if ((dst->y + dst->h) > (src->y + src->h))
227                 dst->h = src->y + src->h - dst->y;
228
229         return EINA_TRUE;
230 }
231
232 static inline void
233 eina_rectangle_rescale_in(const Eina_Rectangle *out, const Eina_Rectangle *in, Eina_Rectangle *res)
234 {
235         res->x = in->x - out->x;
236         res->y = in->y - out->y;
237         res->w = in->w;
238         res->h = in->h;
239 }
240
241 static inline void
242 eina_rectangle_rescale_out(const Eina_Rectangle *out, const Eina_Rectangle *in, Eina_Rectangle *res)
243 {
244         res->x = out->x + in->x;
245         res->y = out->y + in->y;
246         res->w = out->w;
247         res->h = out->h;
248 }
249
250 /**
251  * @}
252  */
253
254 #endif