1 /* EINA - EFL data type library
2 * Copyright (C) 2007-2008 Jorge Luis Zapata Muga
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.
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.
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/>.
19 #ifndef EINA_INLINE_RECTANGLE_H__
20 #define EINA_INLINE_RECTANGLE_H__
23 * @addtogroup Eina_Rectangle_Group Rectangle
25 * @brief These functions provide rectangle management.
31 * @brief Check if the given spans intersect.
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.
39 * This function returns #EINA_TRUE if the given spans intersect, #EINA_FALSE
43 eina_spans_intersect(int c1, int l1, int c2, int l2)
45 return (!(((c2 + l2) <= c1) || (c2 >= (c1 + l1))));
49 * @brief Check if the given rectangle is empty.
51 * @param r The rectangle to check.
52 * @return #EINA_TRUE if the rectangle is empty, #EINA_FALSE otherwise.
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
58 static inline Eina_Bool
59 eina_rectangle_is_empty(const Eina_Rectangle *r)
61 return ((r->w < 1) || (r->h < 1)) ? EINA_TRUE : EINA_FALSE;
65 * @brief Set the coordinates and size of the given rectangle.
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.
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.
78 eina_rectangle_coords_from(Eina_Rectangle *r, int x, int y, int w, int h)
87 * @brief Check if the given rectangles intersect.
89 * @param r1 The first rectangle.
90 * @param r2 The second rectangle.
91 * @return #EINA_TRUE if the rectangles intersect, #EINA_FALSE otherwise.
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
97 static inline Eina_Bool
98 eina_rectangles_intersect(const Eina_Rectangle *r1, const Eina_Rectangle *r2)
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;
104 * @brief Check if the given x-coordinate is in the rectangle .
106 * @param r The rectangle.
107 * @param x The x coordinate.
108 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
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.
114 static inline Eina_Bool
115 eina_rectangle_xcoord_inside(const Eina_Rectangle *r, int x)
117 return ((x >= r->x) && (x < (r->x + r->w))) ? EINA_TRUE : EINA_FALSE;
121 * @brief Check if the given y-coordinate is in the rectangle .
123 * @param r The rectangle.
124 * @param y The y coordinate.
125 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
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.
131 static inline Eina_Bool
132 eina_rectangle_ycoord_inside(const Eina_Rectangle *r, int y)
134 return ((y >= r->y) && (y < (r->y + r->h))) ? EINA_TRUE : EINA_FALSE;
138 * @brief Check if the given point is in the rectangle .
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.
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.
149 static inline Eina_Bool
150 eina_rectangle_coords_inside(const Eina_Rectangle *r, int x, int y)
152 return (eina_rectangle_xcoord_inside(r, x) && eina_rectangle_ycoord_inside(r, y)) ? EINA_TRUE : EINA_FALSE;
156 * @brief Get the union of two rectangles.
158 * @param dst The first rectangle.
159 * @param src The second rectangle.
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.
166 eina_rectangle_union(Eina_Rectangle *dst, const Eina_Rectangle *src)
171 dst->w += dst->x - src->x;
175 if ((dst->x + dst->w) < (src->x + src->w))
176 dst->w = src->x + src->w;
180 dst->h += dst->y - src->y;
184 if ((dst->y + dst->h) < (src->y + src->h))
185 dst->h = src->y + src->h;
189 * @brief Get the intersection of two rectangles.
191 * @param dst The first rectangle.
192 * @param src The second rectangle.
193 * @return #EINA_TRUE if the rectangles intersect, #EINA_FALSE
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.
200 static inline Eina_Bool
201 eina_rectangle_intersection(Eina_Rectangle *dst, const Eina_Rectangle *src)
203 if (!(eina_rectangles_intersect(dst, src)))
209 dst->w += dst->x - src->x;
215 if ((dst->x + dst->w) > (src->x + src->w))
216 dst->w = src->x + src->w - dst->x;
220 dst->h += dst->y - src->y;
226 if ((dst->y + dst->h) > (src->y + src->h))
227 dst->h = src->y + src->h - dst->y;
233 eina_rectangle_rescale_in(const Eina_Rectangle *out, const Eina_Rectangle *in, Eina_Rectangle *res)
235 res->x = in->x - out->x;
236 res->y = in->y - out->y;
242 eina_rectangle_rescale_out(const Eina_Rectangle *out, const Eina_Rectangle *in, Eina_Rectangle *res)
244 res->x = out->x + in->x;
245 res->y = out->y + in->y;