u_rect.h revision 848b8605
1/**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29#ifndef U_RECT_H
30#define U_RECT_H
31
32#include "pipe/p_compiler.h"
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38struct u_rect {
39   int x0, x1;
40   int y0, y1;
41};
42
43/* Do two rectangles intersect?
44 */
45static INLINE boolean
46u_rect_test_intersection(const struct u_rect *a,
47                         const struct u_rect *b)
48{
49   return (!(a->x1 < b->x0 ||
50             b->x1 < a->x0 ||
51             a->y1 < b->y0 ||
52             b->y1 < a->y0));
53}
54
55/* Find the intersection of two rectangles known to intersect.
56 */
57static INLINE void
58u_rect_find_intersection(const struct u_rect *a,
59                         struct u_rect *b)
60{
61   /* Caller should verify intersection exists before calling.
62    */
63   if (b->x0 < a->x0) b->x0 = a->x0;
64   if (b->x1 > a->x1) b->x1 = a->x1;
65   if (b->y0 < a->y0) b->y0 = a->y0;
66   if (b->y1 > a->y1) b->y1 = a->y1;
67}
68
69
70static INLINE void
71u_rect_possible_intersection(const struct u_rect *a,
72                             struct u_rect *b)
73{
74   if (u_rect_test_intersection(a,b)) {
75      u_rect_find_intersection(a,b);
76   }
77   else {
78      b->x0 = b->x1 = b->y0 = b->y1 = 0;
79   }
80}
81
82#ifdef __cplusplus
83}
84#endif
85
86#endif /* U_RECT_H */
87