1 /***********************************************************
3 Copyright 1987, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
26 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
30 Permission to use, copy, modify, and distribute this software and its
31 documentation for any purpose and without fee is hereby granted,
32 provided that the above copyright notice appear in all copies and that
33 both that copyright notice and this permission notice appear in
34 supporting documentation, and that the name of Digital not be
35 used in advertising or publicity pertaining to distribution of the
36 software without specific, written prior permission.
38 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
46 ******************************************************************/
48 #ifdef HAVE_DIX_CONFIG_H
49 #include <dix-config.h>
55 #include "miscanfill.h"
57 static int getPolyYBounds(DDXPointPtr pts
, int n
, int *by
, int *ty
);
62 * Written by Brian Kelleher; Dec. 1985.
64 * Fill a convex polygon. If the given polygon
65 * is not convex, then the result is undefined.
66 * The algorithm is to order the edges from smallest
67 * y to largest by partitioning the array into a left
68 * edge list and a right edge list. The algorithm used
69 * to traverse each edge is an extension of Bresenham's
70 * line algorithm with y as the major axis.
71 * For a derivation of the algorithm, see the author of
75 miFillConvexPoly(dst
, pgc
, count
, ptsIn
)
78 int count
; /* number of points */
79 DDXPointPtr ptsIn
; /* the points */
81 int xl
= 0, xr
= 0; /* x vals of left and right edges */
82 int dl
= 0, dr
= 0; /* decision variables */
83 int ml
= 0, m1l
= 0;/* left edge slope and slope+1 */
84 int mr
= 0, m1r
= 0; /* right edge slope and slope+1 */
85 int incr1l
= 0, incr2l
= 0; /* left edge error increments */
86 int incr1r
= 0, incr2r
= 0; /* right edge error increments */
88 int y
; /* current scanline */
89 int left
, right
; /* indices to first endpoints */
90 int i
; /* loop counter */
91 int nextleft
, nextright
; /* indices to second endpoints */
92 DDXPointPtr ptsOut
, FirstPoint
; /* output buffer */
93 int *width
, *FirstWidth
; /* output buffer */
94 int imin
; /* index of smallest vertex (in y) */
95 int ymin
; /* y-extents of polygon */
99 * find leftx, bottomy, rightx, topy, and the index
100 * of bottomy. Also translate the points.
102 imin
= getPolyYBounds(ptsIn
, count
, &ymin
, &ymax
);
104 dy
= ymax
- ymin
+ 1;
105 if ((count
< 3) || (dy
< 0))
107 ptsOut
= FirstPoint
= (DDXPointPtr
)ALLOCATE_LOCAL(sizeof(DDXPointRec
)*dy
);
108 width
= FirstWidth
= (int *)ALLOCATE_LOCAL(sizeof(int) * dy
);
109 if(!FirstPoint
|| !FirstWidth
)
111 if (FirstWidth
) DEALLOCATE_LOCAL(FirstWidth
);
112 if (FirstPoint
) DEALLOCATE_LOCAL(FirstPoint
);
116 nextleft
= nextright
= imin
;
117 y
= ptsIn
[nextleft
].y
;
120 * loop through all edges of the polygon
124 * add a left edge if we need to
126 if (ptsIn
[nextleft
].y
== y
) {
130 * find the next edge, considering the end
131 * conditions of the array.
134 if (nextleft
>= count
)
138 * now compute all of the random information
139 * needed to run the iterative algorithm.
141 BRESINITPGON(ptsIn
[nextleft
].y
-ptsIn
[left
].y
,
142 ptsIn
[left
].x
,ptsIn
[nextleft
].x
,
143 xl
, dl
, ml
, m1l
, incr1l
, incr2l
);
147 * add a right edge if we need to
149 if (ptsIn
[nextright
].y
== y
) {
153 * find the next edge, considering the end
154 * conditions of the array.
161 * now compute all of the random information
162 * needed to run the iterative algorithm.
164 BRESINITPGON(ptsIn
[nextright
].y
-ptsIn
[right
].y
,
165 ptsIn
[right
].x
,ptsIn
[nextright
].x
,
166 xr
, dr
, mr
, m1r
, incr1r
, incr2r
);
170 * generate scans to fill while we still have
171 * a right edge as well as a left edge.
173 i
= min(ptsIn
[nextleft
].y
, ptsIn
[nextright
].y
) - y
;
174 /* in case we're called with non-convex polygon */
177 DEALLOCATE_LOCAL(FirstWidth
);
178 DEALLOCATE_LOCAL(FirstPoint
);
186 * reverse the edges if necessary
190 *(width
++) = xr
- xl
;
195 *(width
++) = xl
- xr
;
200 /* increment down the edges */
201 BRESINCRPGON(dl
, xl
, ml
, m1l
, incr1l
, incr2l
);
202 BRESINCRPGON(dr
, xr
, mr
, m1r
, incr1r
, incr2r
);
207 * Finally, fill the <remaining> spans
209 (*pgc
->ops
->FillSpans
)(dst
, pgc
,
210 ptsOut
-FirstPoint
,FirstPoint
,FirstWidth
,
212 DEALLOCATE_LOCAL(FirstWidth
);
213 DEALLOCATE_LOCAL(FirstPoint
);
219 * Find the index of the point with the smallest y.
222 getPolyYBounds(DDXPointPtr pts
, int n
, int *by
, int *ty
)
226 DDXPointPtr ptsStart
= pts
;
229 ymin
= ymax
= (pts
++)->y
;
245 return(ptMin
-ptsStart
);