The BIG graph update
[rrdtool.git] / libraries / libart_lgpl-2.3.7 / art_rect.c
1 /* Libart_LGPL - library of basic graphic primitives
2  * Copyright (C) 1998 Raph Levien
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <math.h>
21 #include "art_misc.h"
22 #include "art_rect.h"
23
24 #ifndef MAX
25 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
26 #endif /* MAX */
27
28 #ifndef MIN
29 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
30 #endif /* MIN */
31
32 /* rectangle primitives stolen from gzilla */
33
34 /**
35  * art_irect_copy: Make a copy of an integer rectangle.
36  * @dest: Where the copy is stored.
37  * @src: The source rectangle.
38  *
39  * Copies the rectangle.
40  **/
41 void
42 art_irect_copy (ArtIRect *dest, const ArtIRect *src) {
43   dest->x0 = src->x0;
44   dest->y0 = src->y0;
45   dest->x1 = src->x1;
46   dest->y1 = src->y1;
47 }
48
49 /**
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.
54  *
55  * Finds the smallest rectangle that includes @src1 and @src2.
56  **/
57 void
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);
63   } else {
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);
68   }
69 }
70
71 /**
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.
76  *
77  * Finds the intersection of @src1 and @src2.
78  **/
79 void
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);
85 }
86
87 /**
88  * art_irect_empty: Determine whether integer rectangle is empty.
89  * @src: The source rectangle.
90  *
91  * Return value: TRUE if @src is an empty rectangle, FALSE otherwise.
92  **/
93 int
94 art_irect_empty (const ArtIRect *src) {
95   return (src->x1 <= src->x0 || src->y1 <= src->y0);
96 }
97
98 #if 0
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);
102 }
103 #endif
104
105 /**
106  * art_drect_copy: Make a copy of a rectangle.
107  * @dest: Where the copy is stored.
108  * @src: The source rectangle.
109  *
110  * Copies the rectangle.
111  **/
112 void
113 art_drect_copy (ArtDRect *dest, const ArtDRect *src) {
114   dest->x0 = src->x0;
115   dest->y0 = src->y0;
116   dest->x1 = src->x1;
117   dest->y1 = src->y1;
118 }
119
120 /**
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.
125  *
126  * Finds the smallest rectangle that includes @src1 and @src2.
127  **/
128 void
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);
134   } else {
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);
139   }
140 }
141
142 /**
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.
147  *
148  * Finds the intersection of @src1 and @src2.
149  **/
150 void
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);
156 }
157
158 /**
159  * art_irect_empty: Determine whether rectangle is empty.
160  * @src: The source rectangle.
161  *
162  * Return value: TRUE if @src is an empty rectangle, FALSE otherwise.
163  **/
164 int
165 art_drect_empty (const ArtDRect *src) {
166   return (src->x1 <= src->x0 || src->y1 <= src->y0);
167 }
168
169 /**
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.
174  *
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.
179  **/
180 void
181 art_drect_affine_transform (ArtDRect *dst, const ArtDRect *src, const double matrix[6])
182 {
183   double x00, y00, x10, y10;
184   double x01, y01, x11, y11;
185
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));
198 }
199
200 /**
201  * art_drect_to_irect: Convert rectangle to integer rectangle.
202  * @dst: Where to store resulting integer rectangle.
203  * @src: The source rectangle.
204  *
205  * Find the smallest integer rectangle that encloses @src.
206  **/
207 void
208 art_drect_to_irect (ArtIRect *dst, ArtDRect *src)
209 {
210   dst->x0 = floor (src->x0);
211   dst->y0 = floor (src->y0);
212   dst->x1 = ceil (src->x1);
213   dst->y1 = ceil (src->y1);
214 }