1 /* Copyright (c) 2008 Vincent Povirk
3 Permission is hereby granted, free of charge, to any person
4 obtaining a copy of this software and associated documentation
5 files (the "Software"), to deal in the Software without
6 restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 OTHER DEALINGS IN THE SOFTWARE.
28 #include "lucca-common.h"
31 Internal rectangle type.
32 LuccaRect stores LEFT, RIGHT, TOP, BOTTOM coordinates internally and has
33 operations for manipulating/comparing rectangles without hard-coding
34 special cases for different orientations.
41 /* lucca_rect_from_xywh: Create a rectangle from its xywh coordinates */
42 static LuccaRect
lucca_rect_from_xywh(gint x
, gint y
, gint width
, gint height
) {
43 LuccaRect result
= {{x
, x
+width
-1, y
, y
+height
-1}};
47 /* lucca_rect_getx, lucca_rect_gety, lucca_rect_getwidth, lucca_rect_getheight:
48 Returns an xywh coordinate of the given rectangle */
49 static gint
lucca_rect_getx(LuccaRect r
) {
50 return r
.c
[LUCCA_SIDE_LEFT
];
52 static gint
lucca_rect_gety(LuccaRect r
) {
53 return r
.c
[LUCCA_SIDE_TOP
];
55 static gint
lucca_rect_getwidth(LuccaRect r
) {
56 return r
.c
[LUCCA_SIDE_RIGHT
]-r
.c
[LUCCA_SIDE_LEFT
]+1;
58 static gint
lucca_rect_getheight(LuccaRect r
) {
59 return r
.c
[LUCCA_SIDE_BOTTOM
]-r
.c
[LUCCA_SIDE_TOP
]+1;
62 /* lucca_rect_contains_point: Test if a rectangle contains a point */
63 static gboolean
lucca_rect_contains_point(LuccaRect r
, gint x
, gint y
) {
64 return (r
.c
[LUCCA_SIDE_LEFT
] <= x
&&
65 r
.c
[LUCCA_SIDE_RIGHT
] >= x
&&
66 r
.c
[LUCCA_SIDE_TOP
] <= y
&&
67 r
.c
[LUCCA_SIDE_BOTTOM
] >= y
);
70 /* lucca_rect_contains_rect: Test if a rectangle contains another rectangle */
71 static gboolean
lucca_rect_contains_rect(LuccaRect a
, LuccaRect b
) {
72 return (a
.c
[LUCCA_SIDE_LEFT
] <= b
.c
[LUCCA_SIDE_LEFT
] &&
73 a
.c
[LUCCA_SIDE_RIGHT
] >= b
.c
[LUCCA_SIDE_RIGHT
] &&
74 a
.c
[LUCCA_SIDE_TOP
] <= b
.c
[LUCCA_SIDE_TOP
] &&
75 a
.c
[LUCCA_SIDE_BOTTOM
] >= b
.c
[LUCCA_SIDE_BOTTOM
]);
78 /* lucca_rects_intersect: Test if two rectangles intersect */
79 static gboolean
lucca_rects_intersect(LuccaRect a
, LuccaRect b
) {
80 if (a
.c
[LUCCA_SIDE_LEFT
] > b
.c
[LUCCA_SIDE_RIGHT
])
82 if (b
.c
[LUCCA_SIDE_LEFT
] > a
.c
[LUCCA_SIDE_RIGHT
])
84 if (a
.c
[LUCCA_SIDE_TOP
] > b
.c
[LUCCA_SIDE_BOTTOM
])
86 if (b
.c
[LUCCA_SIDE_TOP
] > a
.c
[LUCCA_SIDE_BOTTOM
])
91 /* lucca_rect_get_thickness: Return a rectangle's thickness in the given direction */
92 static guint
lucca_rect_get_thickness(LuccaRect r
, LuccaSide side
) {
93 return r
.c
[side
|1] - r
.c
[side
&2] + 1;
96 /* lucca_rect_set_thickness: Change rectangle's thickness by changing the given coordinate */
97 static LuccaRect
lucca_rect_set_thickness(LuccaRect r
, LuccaSide side
, guint thickness
) {
98 r
.c
[side
] = r
.c
[lucca_side_opposite(side
)] + lucca_side_sign(side
) * (thickness
- 1);