summaryrefslogtreecommitdiff
path: root/src/lib/eina/eina_inline_rectangle.x
blob: d89d7f1b34f7532e34307801c9d069b27a876c6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/* EINA - EFL data type library
 * Copyright (C) 2007-2008 Jorge Luis Zapata Muga
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#ifndef EINA_INLINE_RECTANGLE_H__
#define EINA_INLINE_RECTANGLE_H__

/**
 * @addtogroup Eina_Rectangle_Group Rectangle
 *
 * @brief These functions provide rectangle management.
 *
 * @{
 */

/**
 * @brief Check if the given spans intersect.
 *
 * @param c1 The column of the first span.
 * @param l1 The length of the first span.
 * @param c2 The column of the second span.
 * @param l2 The length of the second span.
 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
 *
 * This function returns #EINA_TRUE if the  given spans intersect, #EINA_FALSE
 * otherwise.
 */
static inline int
eina_spans_intersect(int c1, int l1, int c2, int l2)
{
	return (!(((c2 + l2) <= c1) || (c2 >= (c1 + l1))));
}

/**
 * @brief Check if the given rectangle is empty.
 *
 * @param r The rectangle to check.
 * @return #EINA_TRUE if the rectangle is empty, #EINA_FALSE otherwise.
 *
 * This function returns #EINA_TRUE if @p r is empty, #EINA_FALSE
 * otherwise. No check is done on @p r, so it must be a valid
 * rectangle.
 */
static inline Eina_Bool
eina_rectangle_is_empty(const Eina_Rectangle *r)
{
	return ((r->w < 1) || (r->h < 1)) ? EINA_TRUE : EINA_FALSE;
}

/**
 * @brief Set the coordinates and size of the given rectangle.
 *
 * @param r The rectangle.
 * @param x The top-left x coordinate of the rectangle.
 * @param y The top-left y coordinate of the rectangle.
 * @param w The width of the rectangle.
 * @param h The height of the rectangle.
 *
 * This function sets its top-left x coordinate to @p x, its top-left
 * y coordinate to @p y, its width to @p w and its height to @p h. No
 * check is done on @p r, so it must be a valid rectangle.
 */
static inline void
eina_rectangle_coords_from(Eina_Rectangle *r, int x, int y, int w, int h)
{
	r->x = x;
	r->y = y;
	r->w = w;
	r->h = h;
}

/**
 * @brief Check if the given rectangles intersect.
 *
 * @param r1 The first rectangle.
 * @param r2 The second rectangle.
 * @return #EINA_TRUE if the rectangles intersect, #EINA_FALSE otherwise.
 *
 * This function returns #EINA_TRUE if @p r1 and @p r2 intersect, #EINA_FALSE
 * otherwise. No check is done on @p r1 and @p r2, so they must be valid
 * rectangles.
 */
static inline Eina_Bool
eina_rectangles_intersect(const Eina_Rectangle *r1, const Eina_Rectangle *r2)
{
	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;
}

/**
 * @brief Check if the given x-coordinate is in the rectangle .
 *
 * @param r The rectangle.
 * @param x The x coordinate.
 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
 *
 * This function returns #EINA_TRUE if @p x is in @p r with respect to
 * the horizontal direction, #EINA_FALSE otherwise. No check is done
 * on @p r, so it must be a valid rectangle.
 */
static inline Eina_Bool
eina_rectangle_xcoord_inside(const Eina_Rectangle *r, int x)
{
	return ((x >= r->x) && (x < (r->x + r->w))) ? EINA_TRUE : EINA_FALSE;
}

/**
 * @brief Check if the given y-coordinate is in the rectangle .
 *
 * @param r The rectangle.
 * @param y The y coordinate.
 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
 *
 * This function returns #EINA_TRUE if @p y is in @p r with respect to
 * the vertical direction, #EINA_FALSE otherwise. No check is done
 * on @p r, so it must be a valid rectangle.
 */
static inline Eina_Bool
eina_rectangle_ycoord_inside(const Eina_Rectangle *r, int y)
{
	return ((y >= r->y) && (y < (r->y + r->h))) ? EINA_TRUE : EINA_FALSE;
}

/**
 * @brief Check if the given point is in the rectangle .
 *
 * @param r The rectangle.
 * @param x The x coordinate of the point.
 * @param y The y coordinate of the point.
 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
 *
 * This function returns #EINA_TRUE if the point of coordinate (@p x,
 * @p y) is in @p r, #EINA_FALSE otherwise. No check is done on @p r,
 * so it must be a valid rectangle.
 */
static inline Eina_Bool
eina_rectangle_coords_inside(const Eina_Rectangle *r, int x, int y)
{
	return (eina_rectangle_xcoord_inside(r, x) && eina_rectangle_ycoord_inside(r, y)) ? EINA_TRUE : EINA_FALSE;
}

/**
 * @brief Get the union of two rectangles.
 *
 * @param dst The first rectangle.
 * @param src The second rectangle.
 *
 * This function get the union of the rectangles @p dst and @p src. The
 * result is stored in @p dst. No check is done on @p dst or @p src,
 * so they must be valid rectangles.
 */
static inline void
eina_rectangle_union(Eina_Rectangle *dst, const Eina_Rectangle *src)
{
	/* left */
	if (dst->x > src->x)
	{
		dst->w += dst->x - src->x;
		dst->x = src->x;
	}
	/* right */
	if ((dst->x + dst->w) < (src->x + src->w))
		dst->w = src->x + src->w;
	/* top */
	if (dst->y > src->y)
	{
		dst->h += dst->y - src->y;
		dst->y = src->y;
	}
	/* bottom */
	if ((dst->y + dst->h) < (src->y + src->h))
		dst->h = src->y + src->h;
}

/**
 * @brief Get the intersection of two rectangles.
 *
 * @param dst The first rectangle.
 * @param src The second rectangle.
 * @return #EINA_TRUE if the rectangles intersect, #EINA_FALSE
 * otherwise.
 *
 * This function get the intersection of the rectangles @p dst and
 * @p src. The result is stored in @p dst. No check is done on @p dst
 * or @p src, so they must be valid rectangles.
 */
static inline Eina_Bool
eina_rectangle_intersection(Eina_Rectangle *dst, const Eina_Rectangle *src)
{
	if (!(eina_rectangles_intersect(dst, src)))
		return EINA_FALSE;

	/* left */
	if (dst->x < src->x)
	{
		dst->w += dst->x - src->x;
		dst->x = src->x;
		if (dst->w < 0)
			dst->w = 0;
	}
	/* right */
	if ((dst->x + dst->w) > (src->x + src->w))
		dst->w = src->x + src->w - dst->x;
	/* top */
	if (dst->y < src->y)
	{
		dst->h += dst->y - src->y;
		dst->y = src->y;
		if (dst->h < 0)
			dst->h = 0;
	}
	/* bottom */
	if ((dst->y + dst->h) > (src->y + src->h))
		dst->h = src->y + src->h - dst->y;

	return EINA_TRUE;
}

static inline void
eina_rectangle_rescale_in(const Eina_Rectangle *out, const Eina_Rectangle *in, Eina_Rectangle *res)
{
	res->x = in->x - out->x;
	res->y = in->y - out->y;
	res->w = in->w;
	res->h = in->h;
}

static inline void
eina_rectangle_rescale_out(const Eina_Rectangle *out, const Eina_Rectangle *in, Eina_Rectangle *res)
{
	res->x = out->x + in->x;
	res->y = out->y + in->y;
	res->w = out->w;
	res->h = out->h;
}

static inline Eina_Bool
eina_rectangle_is_valid(const Eina_Rectangle *r)
{
	if (r->w <= 0 || r->h <= 0)
		return EINA_FALSE;
	return EINA_TRUE;
}

static inline int
eina_rectangle_max_x(Eina_Rectangle *thiz)
{
	return thiz->x + thiz->w;
}

static inline int
eina_rectangle_max_y(Eina_Rectangle *thiz)
{
	return thiz->y + thiz->h;
}

static inline Eina_Bool
eina_rectangle_x_cut(Eina_Rectangle *thiz, Eina_Rectangle *slice, Eina_Rectangle *leftover, int amount)
{
	Eina_Rectangle tmp1, tmp2;
	if (amount > thiz->w)
		return EINA_FALSE;
	eina_rectangle_coords_from(&tmp1, thiz->x, thiz->y, amount, thiz->h);
	eina_rectangle_coords_from(&tmp2, thiz->x + amount, thiz->y, thiz->w - amount, thiz->h);
	if (slice) *slice = tmp1;
	if (leftover) *leftover = tmp2;
	return EINA_TRUE;
}

static inline Eina_Bool
eina_rectangle_y_cut(Eina_Rectangle *thiz, Eina_Rectangle *slice, Eina_Rectangle *leftover, int amount)
{
	Eina_Rectangle tmp1, tmp2;
	if (amount > thiz->h)
		return EINA_FALSE;
	eina_rectangle_coords_from(&tmp1, thiz->x, thiz->y, thiz->w, amount);
	eina_rectangle_coords_from(&tmp2, thiz->x, thiz->y + amount, thiz->w, thiz->h - amount);
	if (slice) *slice = tmp1;
	if (leftover) *leftover = tmp2;
	return EINA_TRUE;
}

static inline Eina_Bool
eina_rectangle_width_cut(Eina_Rectangle *thiz, Eina_Rectangle *slice, Eina_Rectangle *leftover, int amount)
{
	Eina_Rectangle tmp1, tmp2;
	if (thiz->w - amount < 0)
		return EINA_FALSE;
	eina_rectangle_coords_from(&tmp1, thiz->x + (thiz->w - amount), thiz->y, amount, thiz->h);
	eina_rectangle_coords_from(&tmp2, thiz->x, thiz->y, thiz->w - amount, thiz->h);
	if (slice) *slice = tmp1;
	if (leftover) *leftover = tmp2;
	return EINA_TRUE;
}

static inline Eina_Bool
eina_rectangle_height_cut(Eina_Rectangle *thiz, Eina_Rectangle *slice, Eina_Rectangle *leftover, int amount)
{
	Eina_Rectangle tmp1, tmp2;
	if (thiz->h - amount < 0)
		return EINA_FALSE;
	eina_rectangle_coords_from(&tmp1, thiz->x, thiz->y + (thiz->h - amount), thiz->w, amount);
	eina_rectangle_coords_from(&tmp2, thiz->x, thiz->y, thiz->w, thiz->h - amount);
	if (slice) *slice = tmp1;
	if (leftover) *leftover = tmp2;
	return EINA_TRUE;
}

/**
 * @brief Subtract two rectangles.
 *
 * @param thiz The minuend rectangle
 * @param other The subtrahend rectangle
 *
 * This function subtract two rectangles. The difference is stored on @p out
 * There will be at most four differences, use eina_rectangle_is_valid to
 * confirm the number of differences.
 */
static inline Eina_Bool
eina_rectangle_subtract(Eina_Rectangle *thiz, Eina_Rectangle *other, Eina_Rectangle out[4])
{
	Eina_Rectangle intersection;
	Eina_Rectangle leftover = EINA_RECTANGLE_INIT;
	Eina_Rectangle tmp;
	int cut = 0;

	if (!eina_rectangle_is_valid(thiz))
		return EINA_FALSE;

	eina_rectangle_coords_from(&out[0], 0, 0, 0, 0);
	eina_rectangle_coords_from(&out[1], 0, 0, 0, 0);
	eina_rectangle_coords_from(&out[2], 0, 0, 0, 0);
	eina_rectangle_coords_from(&out[3], 0, 0, 0, 0);
	intersection = *thiz;
	if (!eina_rectangle_intersection(&intersection, other))
	{
		out[0] = *thiz;
		return EINA_TRUE;
	}

	/* cut in height */
	{
		cut = thiz->h - (intersection.y - thiz->y);
		if (cut > thiz->h) { cut = thiz->h; }
		eina_rectangle_height_cut(thiz, &leftover, &out[0], cut);
	}
	/* cut in y */
	tmp = leftover;
	if (eina_rectangle_intersection(&tmp, &intersection))
	{
		cut = leftover.h - (eina_rectangle_max_y(&leftover) - eina_rectangle_max_y(&tmp));
		if (cut > leftover.h) { cut = leftover.h; }
		eina_rectangle_y_cut(&leftover, &leftover, &out[1], cut);
	}
	/* cut in width */
	tmp = leftover;
	if (eina_rectangle_intersection(&tmp, &intersection))
	{
		cut = leftover.w - (tmp.x - leftover.x);
		if (cut > leftover.w) { cut = leftover.w; }
		eina_rectangle_width_cut(&leftover, &leftover, &out[2], cut);
	}
	/* cut in x */
	tmp = leftover;
	if (eina_rectangle_intersection(&tmp, &intersection))
	{
		cut = leftover.w - (eina_rectangle_max_x(&leftover) - eina_rectangle_max_x(&tmp));
		if (cut > leftover.w) { cut = leftover.w; }
		eina_rectangle_x_cut(&leftover, &leftover, &out[3], cut);
	}

	return EINA_TRUE;
}

/**
 * @}
 */

#endif