1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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 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; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gdkrectangle.h"
34 * @Short_description: Simple graphical data types
35 * @Title: Points and Rectangles
37 * GDK provides the #GdkPoint and #GdkRectangle data types for representing pixels
38 * and sets of pixels on the screen. Together with Cairo's #cairo_region_t data
39 * type, they make up the central types for representing graphical data.
41 * #GdkPoint is a simple structure containing an x and y coordinate of a point.
43 * #GdkRectangle is a structure holding the position and size of a rectangle.
44 * The intersection of two rectangles can be computed with
45 * gdk_rectangle_intersect(). To find the union of two rectangles use
46 * gdk_rectangle_union().
48 * #cairo_region_t is usually used for managing clipping of graphical operations.
53 * gdk_rectangle_union:
54 * @src1: a #GdkRectangle
55 * @src2: a #GdkRectangle
56 * @dest: return location for the union of @src1 and @src2
58 * Calculates the union of two rectangles.
59 * The union of rectangles @src1 and @src2 is the smallest rectangle which
60 * includes both @src1 and @src2 within it.
61 * It is allowed for @dest to be the same as either @src1 or @src2.
64 gdk_rectangle_union (const GdkRectangle *src1,
65 const GdkRectangle *src2,
70 g_return_if_fail (src1 != NULL);
71 g_return_if_fail (src2 != NULL);
72 g_return_if_fail (dest != NULL);
74 dest_x = MIN (src1->x, src2->x);
75 dest_y = MIN (src1->y, src2->y);
76 dest->width = MAX (src1->x + src1->width, src2->x + src2->width) - dest_x;
77 dest->height = MAX (src1->y + src1->height, src2->y + src2->height) - dest_y;
83 * gdk_rectangle_intersect:
84 * @src1: a #GdkRectangle
85 * @src2: a #GdkRectangle
86 * @dest: (out caller-allocates) (allow-none): return location for the
87 * intersection of @src1 and @src2, or %NULL
89 * Calculates the intersection of two rectangles. It is allowed for
90 * @dest to be the same as either @src1 or @src2. If the rectangles
91 * do not intersect, @dest's width and height is set to 0 and its x
92 * and y values are undefined. If you are only interested in whether
93 * the rectangles intersect, but not in the intersecting area itself,
94 * pass %NULL for @dest.
96 * Returns: %TRUE if the rectangles intersect.
99 gdk_rectangle_intersect (const GdkRectangle *src1,
100 const GdkRectangle *src2,
104 gint dest_x2, dest_y2;
107 g_return_val_if_fail (src1 != NULL, FALSE);
108 g_return_val_if_fail (src2 != NULL, FALSE);
112 dest_x = MAX (src1->x, src2->x);
113 dest_y = MAX (src1->y, src2->y);
114 dest_x2 = MIN (src1->x + src1->width, src2->x + src2->width);
115 dest_y2 = MIN (src1->y + src1->height, src2->y + src2->height);
117 if (dest_x2 > dest_x && dest_y2 > dest_y)
123 dest->width = dest_x2 - dest_x;
124 dest->height = dest_y2 - dest_y;
137 static GdkRectangle *
138 gdk_rectangle_copy (const GdkRectangle *rectangle)
140 GdkRectangle *result = g_new (GdkRectangle, 1);
141 *result = *rectangle;
146 G_DEFINE_BOXED_TYPE (GdkRectangle, gdk_rectangle,