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: mifpolycon.c,v 5.4 94/04/17 20:27:36 rws Exp $ */
52 #include "windowstr.h"
53 #include "pixmapstr.h"
56 static int GetFPolyYBounds();
63 * Written by Todd Newman; April. 1987.
65 * Fill a convex polygon. If the given polygon
66 * is not convex, then the result is undefined.
67 * The algorithm is to order the edges from smallest
68 * y to largest by partitioning the array into a left
69 * edge list and a right edge list. The algorithm used
70 * to traverse each edge is digital differencing analyzer
71 * line algorithm with y as the major axis. There's some funny linear
72 * interpolation involved because of the subpixel postioning.
75 miFillSppPoly(dst, pgc, count, ptsIn, xTrans, yTrans, xFtrans, yFtrans)
78 int count; /* number of points */
79 SppPointPtr ptsIn; /* the points */
80 int xTrans, yTrans; /* Translate each point by this */
81 double xFtrans, yFtrans; /* translate before conversion
82 by this amount. This provides
83 a mechanism to match rounding
84 errors with any shape that must
85 meet the polygon exactly.
88 double xl, xr, /* x vals of left and right edges */
89 ml, /* left edge slope */
90 mr, /* right edge slope */
93 int y, /* current scanline */
95 imin, /* index of vertex with smallest y */
96 ymin, /* y-extents of polygon */
99 *FirstWidth, /* output buffer */
100 *Marked; /* set if this vertex has been used */
101 register int left, right, /* indices to first endpoints */
103 nextright; /* indices to second endpoints */
105 FirstPoint; /* output buffer */
107 if (pgc->miTranslate)
113 imin = GetFPolyYBounds(ptsIn, count, yFtrans, &ymin, &ymax);
116 if ((count < 3) || (y <= 0))
118 ptsOut = FirstPoint = (DDXPointPtr)ALLOCATE_LOCAL(sizeof(DDXPointRec) * y);
119 width = FirstWidth = (int *) ALLOCATE_LOCAL(sizeof(int) * y);
120 Marked = (int *) ALLOCATE_LOCAL(sizeof(int) * count);
122 if(!ptsOut || !width || !Marked)
124 if (Marked) DEALLOCATE_LOCAL(Marked);
125 if (width) DEALLOCATE_LOCAL(width);
126 if (ptsOut) DEALLOCATE_LOCAL(ptsOut);
130 for(j = 0; j < count; j++)
132 nextleft = nextright = imin;
134 y = ICEIL(ptsIn[nextleft].y + yFtrans);
137 * loop through all edges of the polygon
141 /* add a left edge if we need to */
142 if ((y > (ptsIn[nextleft].y + yFtrans) ||
143 ISEQUAL(y, ptsIn[nextleft].y + yFtrans)) &&
144 Marked[nextleft] != 1)
149 /* find the next edge, considering the end conditions */
150 if (nextleft >= count)
153 /* now compute the starting point and slope */
154 dy = ptsIn[nextleft].y - ptsIn[left].y;
157 ml = (ptsIn[nextleft].x - ptsIn[left].x) / dy;
158 dy = y - (ptsIn[left].y + yFtrans);
159 xl = (ptsIn[left].x + xFtrans) + ml * max(dy, 0);
163 /* add a right edge if we need to */
164 if ((y > ptsIn[nextright].y + yFtrans) ||
165 ISEQUAL(y, ptsIn[nextright].y + yFtrans)
166 && Marked[nextright] != 1)
171 /* find the next edge, considering the end conditions */
173 nextright = count - 1;
175 /* now compute the starting point and slope */
176 dy = ptsIn[nextright].y - ptsIn[right].y;
179 mr = (ptsIn[nextright].x - ptsIn[right].x) / dy;
180 dy = y - (ptsIn[right].y + yFtrans);
181 xr = (ptsIn[right].x + xFtrans) + mr * max(dy, 0);
187 * generate scans to fill while we still have
188 * a right edge as well as a left edge.
190 i = (min(ptsIn[nextleft].y, ptsIn[nextright].y) + yFtrans) - y;
194 if(Marked[nextleft] && Marked[nextright])
196 /* Arrgh, we're trapped! (no more points)
197 * Out, we've got to get out of here before this decadence saps
198 * our will completely! */
213 ptsOut->y = (y) + yTrans;
217 /* reverse the edges if necessary */
220 *(width++) = cxr - cxl;
221 (ptsOut++)->x = cxl + xTrans;
225 *(width++) = cxl - cxr;
226 (ptsOut++)->x = cxr + xTrans;
230 /* increment down the edges */
237 /* Finally, fill the spans we've collected */
238 (*pgc->ops->FillSpans)(dst, pgc,
239 ptsOut-FirstPoint, FirstPoint, FirstWidth, 1);
240 DEALLOCATE_LOCAL(Marked);
241 DEALLOCATE_LOCAL(FirstWidth);
242 DEALLOCATE_LOCAL(FirstPoint);
246 /* Find the index of the point with the smallest y.also return the
247 * smallest and largest y */
250 GetFPolyYBounds(pts, n, yFtrans, by, ty)
251 register SppPointPtr pts;
256 register SppPointPtr ptMin;
258 SppPointPtr ptsStart = pts;
261 ymin = ymax = (pts++)->y;
275 *by = ICEIL(ymin + yFtrans);
276 *ty = ICEIL(ymax + yFtrans - 1);
277 return(ptMin-ptsStart);