1 /***********************************************************
3 Copyright (c) 1987 X Consortium
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 Except as contained in this notice, the name of the X Consortium shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from the X Consortium.
27 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
31 Permission to use, copy, modify, and distribute this software and its
32 documentation for any purpose and without fee is hereby granted,
33 provided that the above copyright notice appear in all copies and that
34 both that copyright notice and this permission notice appear in
35 supporting documentation, and that the name of Digital not be
36 used in advertising or publicity pertaining to distribution of the
37 software without specific, written prior permission.
39 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
40 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
41 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
42 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
43 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
44 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
47 ******************************************************************/
48 /* $XConsortium: mipolycon.c,v 5.1 94/04/17 20:27:43 keith Exp $ */
51 #include "miscanfill.h"
53 static int getPolyYBounds();
58 * Written by Brian Kelleher; Dec. 1985.
60 * Fill a convex polygon. If the given polygon
61 * is not convex, then the result is undefined.
62 * The algorithm is to order the edges from smallest
63 * y to largest by partitioning the array into a left
64 * edge list and a right edge list. The algorithm used
65 * to traverse each edge is an extension of Bresenham's
66 * line algorithm with y as the major axis.
67 * For a derivation of the algorithm, see the author of
71 miFillConvexPoly(dst, pgc, count, ptsIn)
74 int count; /* number of points */
75 DDXPointPtr ptsIn; /* the points */
77 register int xl, xr; /* x vals of left and right edges */
78 register int dl, dr; /* decision variables */
79 register int ml, m1l; /* left edge slope and slope+1 */
80 int mr, m1r; /* right edge slope and slope+1 */
81 int incr1l, incr2l; /* left edge error increments */
82 int incr1r, incr2r; /* right edge error increments */
84 int y; /* current scanline */
85 int left, right; /* indices to first endpoints */
86 int i; /* loop counter */
87 int nextleft, nextright; /* indices to second endpoints */
88 DDXPointPtr ptsOut, FirstPoint; /* output buffer */
89 int *width, *FirstWidth; /* output buffer */
90 int imin; /* index of smallest vertex (in y) */
91 int ymin; /* y-extents of polygon */
95 * find leftx, bottomy, rightx, topy, and the index
96 * of bottomy. Also translate the points.
98 imin = getPolyYBounds(ptsIn, count, &ymin, &ymax);
100 dy = ymax - ymin + 1;
101 if ((count < 3) || (dy < 0))
103 ptsOut = FirstPoint = (DDXPointPtr )ALLOCATE_LOCAL(sizeof(DDXPointRec)*dy);
104 width = FirstWidth = (int *)ALLOCATE_LOCAL(sizeof(int) * dy);
105 if(!FirstPoint || !FirstWidth)
107 if (FirstWidth) DEALLOCATE_LOCAL(FirstWidth);
108 if (FirstPoint) DEALLOCATE_LOCAL(FirstPoint);
112 nextleft = nextright = imin;
113 y = ptsIn[nextleft].y;
116 * loop through all edges of the polygon
120 * add a left edge if we need to
122 if (ptsIn[nextleft].y == y) {
126 * find the next edge, considering the end
127 * conditions of the array.
130 if (nextleft >= count)
134 * now compute all of the random information
135 * needed to run the iterative algorithm.
137 BRESINITPGON(ptsIn[nextleft].y-ptsIn[left].y,
138 ptsIn[left].x,ptsIn[nextleft].x,
139 xl, dl, ml, m1l, incr1l, incr2l);
143 * add a right edge if we need to
145 if (ptsIn[nextright].y == y) {
149 * find the next edge, considering the end
150 * conditions of the array.
157 * now compute all of the random information
158 * needed to run the iterative algorithm.
160 BRESINITPGON(ptsIn[nextright].y-ptsIn[right].y,
161 ptsIn[right].x,ptsIn[nextright].x,
162 xr, dr, mr, m1r, incr1r, incr2r);
166 * generate scans to fill while we still have
167 * a right edge as well as a left edge.
169 i = min(ptsIn[nextleft].y, ptsIn[nextright].y) - y;
170 /* in case we're called with non-convex polygon */
173 DEALLOCATE_LOCAL(FirstWidth);
174 DEALLOCATE_LOCAL(FirstPoint);
182 * reverse the edges if necessary
186 *(width++) = xr - xl;
191 *(width++) = xl - xr;
196 /* increment down the edges */
197 BRESINCRPGON(dl, xl, ml, m1l, incr1l, incr2l);
198 BRESINCRPGON(dr, xr, mr, m1r, incr1r, incr2r);
203 * Finally, fill the <remaining> spans
205 (*pgc->ops->FillSpans)(dst, pgc,
206 ptsOut-FirstPoint,FirstPoint,FirstWidth,
208 DEALLOCATE_LOCAL(FirstWidth);
209 DEALLOCATE_LOCAL(FirstPoint);
215 * Find the index of the point with the smallest y.
219 getPolyYBounds(pts, n, by, ty)
224 register DDXPointPtr ptMin;
226 DDXPointPtr ptsStart = pts;
229 ymin = ymax = (pts++)->y;
245 return(ptMin-ptsStart);