1 /* Libart_LGPL - library of basic graphic primitives
2 * Copyright (C) 1998 Raph Levien
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
25 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
29 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
32 /* rectangle primitives stolen from gzilla */
35 * art_irect_copy: Make a copy of an integer rectangle.
36 * @dest: Where the copy is stored.
37 * @src: The source rectangle.
39 * Copies the rectangle.
42 art_irect_copy (ArtIRect *dest, const ArtIRect *src) {
50 * art_irect_union: Find union of two integer rectangles.
51 * @dest: Where the result is stored.
52 * @src1: A source rectangle.
53 * @src2: Another source rectangle.
55 * Finds the smallest rectangle that includes @src1 and @src2.
58 art_irect_union (ArtIRect *dest, const ArtIRect *src1, const ArtIRect *src2) {
59 if (art_irect_empty (src1)) {
60 art_irect_copy (dest, src2);
61 } else if (art_irect_empty (src2)) {
62 art_irect_copy (dest, src1);
64 dest->x0 = MIN (src1->x0, src2->x0);
65 dest->y0 = MIN (src1->y0, src2->y0);
66 dest->x1 = MAX (src1->x1, src2->x1);
67 dest->y1 = MAX (src1->y1, src2->y1);
72 * art_irect_intersection: Find intersection of two integer rectangles.
73 * @dest: Where the result is stored.
74 * @src1: A source rectangle.
75 * @src2: Another source rectangle.
77 * Finds the intersection of @src1 and @src2.
80 art_irect_intersect (ArtIRect *dest, const ArtIRect *src1, const ArtIRect *src2) {
81 dest->x0 = MAX (src1->x0, src2->x0);
82 dest->y0 = MAX (src1->y0, src2->y0);
83 dest->x1 = MIN (src1->x1, src2->x1);
84 dest->y1 = MIN (src1->y1, src2->y1);
88 * art_irect_empty: Determine whether integer rectangle is empty.
89 * @src: The source rectangle.
91 * Return value: TRUE if @src is an empty rectangle, FALSE otherwise.
94 art_irect_empty (const ArtIRect *src) {
95 return (src->x1 <= src->x0 || src->y1 <= src->y0);
99 gboolean irect_point_inside (ArtIRect *rect, GzwPoint *point) {
100 return (point->x >= rect->x0 && point->y >= rect->y0 &&
101 point->x < rect->x1 && point->y < rect->y1);
106 * art_drect_copy: Make a copy of a rectangle.
107 * @dest: Where the copy is stored.
108 * @src: The source rectangle.
110 * Copies the rectangle.
113 art_drect_copy (ArtDRect *dest, const ArtDRect *src) {
121 * art_drect_union: Find union of two rectangles.
122 * @dest: Where the result is stored.
123 * @src1: A source rectangle.
124 * @src2: Another source rectangle.
126 * Finds the smallest rectangle that includes @src1 and @src2.
129 art_drect_union (ArtDRect *dest, const ArtDRect *src1, const ArtDRect *src2) {
130 if (art_drect_empty (src1)) {
131 art_drect_copy (dest, src2);
132 } else if (art_drect_empty (src2)) {
133 art_drect_copy (dest, src1);
135 dest->x0 = MIN (src1->x0, src2->x0);
136 dest->y0 = MIN (src1->y0, src2->y0);
137 dest->x1 = MAX (src1->x1, src2->x1);
138 dest->y1 = MAX (src1->y1, src2->y1);
143 * art_drect_intersection: Find intersection of two rectangles.
144 * @dest: Where the result is stored.
145 * @src1: A source rectangle.
146 * @src2: Another source rectangle.
148 * Finds the intersection of @src1 and @src2.
151 art_drect_intersect (ArtDRect *dest, const ArtDRect *src1, const ArtDRect *src2) {
152 dest->x0 = MAX (src1->x0, src2->x0);
153 dest->y0 = MAX (src1->y0, src2->y0);
154 dest->x1 = MIN (src1->x1, src2->x1);
155 dest->y1 = MIN (src1->y1, src2->y1);
159 * art_irect_empty: Determine whether rectangle is empty.
160 * @src: The source rectangle.
162 * Return value: TRUE if @src is an empty rectangle, FALSE otherwise.
165 art_drect_empty (const ArtDRect *src) {
166 return (src->x1 <= src->x0 || src->y1 <= src->y0);
170 * art_drect_affine_transform: Affine transform rectangle.
171 * @dst: Where to store the result.
172 * @src: The source rectangle.
173 * @matrix: The affine transformation.
175 * Find the smallest rectangle enclosing the affine transformed @src.
176 * The result is exactly the affine transformation of @src when
177 * @matrix specifies a rectilinear affine transformation, otherwise it
178 * is a conservative approximation.
181 art_drect_affine_transform (ArtDRect *dst, const ArtDRect *src, const double matrix[6])
183 double x00, y00, x10, y10;
184 double x01, y01, x11, y11;
186 x00 = src->x0 * matrix[0] + src->y0 * matrix[2] + matrix[4];
187 y00 = src->x0 * matrix[1] + src->y0 * matrix[3] + matrix[5];
188 x10 = src->x1 * matrix[0] + src->y0 * matrix[2] + matrix[4];
189 y10 = src->x1 * matrix[1] + src->y0 * matrix[3] + matrix[5];
190 x01 = src->x0 * matrix[0] + src->y1 * matrix[2] + matrix[4];
191 y01 = src->x0 * matrix[1] + src->y1 * matrix[3] + matrix[5];
192 x11 = src->x1 * matrix[0] + src->y1 * matrix[2] + matrix[4];
193 y11 = src->x1 * matrix[1] + src->y1 * matrix[3] + matrix[5];
194 dst->x0 = MIN (MIN (x00, x10), MIN (x01, x11));
195 dst->y0 = MIN (MIN (y00, y10), MIN (y01, y11));
196 dst->x1 = MAX (MAX (x00, x10), MAX (x01, x11));
197 dst->y1 = MAX (MAX (y00, y10), MAX (y01, y11));
201 * art_drect_to_irect: Convert rectangle to integer rectangle.
202 * @dst: Where to store resulting integer rectangle.
203 * @src: The source rectangle.
205 * Find the smallest integer rectangle that encloses @src.
208 art_drect_to_irect (ArtIRect *dst, ArtDRect *src)
210 dst->x0 = floor (src->x0);
211 dst->y0 = floor (src->y0);
212 dst->x1 = ceil (src->x1);
213 dst->y1 = ceil (src->y1);