xref: /plan9/sys/src/cmd/gs/src/gsrect.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gsrect.h,v 1.5 2002/06/16 08:45:42 lpd Exp $ */
18 /* Rectangle utilities */
19 
20 #ifndef gsrect_INCLUDED
21 #  define gsrect_INCLUDED
22 
23 #include "gxfixed.h"
24 
25 /* Check whether one rectangle is included entirely within another. */
26 #define rect_within(inner, outer)\
27   ((inner).q.y <= (outer).q.y && (inner).q.x <= (outer).q.x &&\
28    (inner).p.y >= (outer).p.y && (inner).p.x >= (outer).p.x)
29 
30 /*
31  * Intersect two rectangles, replacing the first.  The result may be
32  * anomalous (q < p) if the intersection is empty.
33  */
34 #define rect_intersect(to, from)\
35   BEGIN\
36     if ((from).p.x > (to).p.x) (to).p.x = (from).p.x;\
37     if ((from).q.x < (to).q.x) (to).q.x = (from).q.x;\
38     if ((from).p.y > (to).p.y) (to).p.y = (from).p.y;\
39     if ((from).q.y < (to).q.y) (to).q.y = (from).q.y;\
40   END
41 
42 /*
43  * Merge two rectangles, replacing the first.  The result may be
44  * anomalous (q < p) if the first rectangle was anomalous.
45  */
46 #define rect_merge(to, from)\
47   BEGIN\
48     if ((from).p.x < (to).p.x) (to).p.x = (from).p.x;\
49     if ((from).q.x > (to).q.x) (to).q.x = (from).q.x;\
50     if ((from).p.y < (to).p.y) (to).p.y = (from).p.y;\
51     if ((from).q.y > (to).q.y) (to).q.y = (from).q.y;\
52   END
53 
54 /*
55  * Calculate the difference of two rectangles, a list of up to 4 rectangles.
56  * Return the number of rectangles in the list, and set the first rectangle
57  * to the intersection.  The resulting first rectangle is guaranteed not to
58  * be anomalous (q < p) iff it was not anomalous originally.
59  *
60  * Note that unlike the macros above, we need different versions of this
61  * depending on the data type of the individual values: we'll only implement
62  * the variations that we need.
63  */
64 int int_rect_difference(gs_int_rect * outer, const gs_int_rect * inner,
65 			gs_int_rect * diffs /*[4] */ );
66 
67 /*
68  * Check whether a parallelogram is a rectangle.
69  */
70 #define PARALLELOGRAM_IS_RECT(ax, ay, bx, by)\
71   ( ((ax) | (by)) == 0 || ((bx) | (ay)) == 0 )
72 
73 /*
74  * Convert a rectangular parallelogram to a rectangle, carefully following
75  * the center-of-pixel rule in all cases.
76  */
77 #define INT_RECT_FROM_PARALLELOGRAM(prect, px, py, ax, ay, bx, by)\
78   BEGIN\
79     int px_ = fixed2int_pixround(px);\
80     int py_ = fixed2int_pixround(py);\
81     int qx_ = fixed2int_pixround((px) + (ax) + (bx));  /* only one is non-zero */\
82     int qy_ = fixed2int_pixround((py) + (ay) + (by));  /* ditto */\
83 \
84     if (qx_ < px_)\
85       (prect)->p.x = qx_, (prect)->q.x = px_;\
86     else\
87       (prect)->p.x = px_, (prect)->q.x = qx_;\
88     if (qy_ < py_)\
89       (prect)->p.y = qy_, (prect)->q.y = py_;\
90     else\
91       (prect)->p.y = py_, (prect)->q.y = qy_;\
92   END
93 
94 #endif /* gsrect_INCLUDED */
95