forked from ghaerr/microwindows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevpoly.c
More file actions
698 lines (621 loc) · 20 KB
/
Copy pathdevpoly.c
File metadata and controls
698 lines (621 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#include <stdlib.h>
#include "uni_std.h"
#include "device.h"
/*
* Microwindows polygon outline and fill routines.
* Copyright (c) 1999, 2000, 2001, 2002 Greg Haerr <greg@censoft.com>
* Portions Copyright (c) 1991 David I. Bell
*
* There are currently three implementations of the polygon
* fill routine. The desired routine is set using a
* #define after this comment.
* Issues with regards to polygon fill are 1) whether concave
* filling is supported, and 2) whether outlines match the
* polygon fill bresenham lines. Each routine differs in these regards.
*
* X11POLYFILL most properly fills polygons that must also be
* outlined as well.
* EDGEPOLYFILL fills concave polygons, but outlines don't
* exactly match up. Can use floating point, if set in device.h.
* BASICPOLYFILL won't fill concave polygons, but is small.
*
* FIXME - X11POLYFILL fails with the concave poly fills
* in demos/nanox/polydemo.c
*/
/* set polygon fill routine*/
#define EDGEPOLYFILL 1 /* edge table, malloc, qsort*/
#define X11POLYFILL 0 /* X11-derived polygon fill*/
#define BASICPOLYFILL 0 /* very basic, small polygon fill*/
/* extern definitions*/
extern int gr_mode; /* drawing mode */
extern int gr_fillmode;
/**
* Draw a polygon in the foreground color, applying clipping if necessary.
* The polygon is only closed if the first point is repeated at the end.
* Some care is taken to plot the endpoints correctly if the current
* drawing mode is XOR. However, internal crossings are not handled
* correctly.
*
* @param psd Drawing surface.
* @param count Number of points in polygon.
* @param points The array of points.
*/
void
GdPoly(PSD psd, int count, MWPOINT *points)
{
MWCOORD firstx;
MWCOORD firsty;
MWBOOL didline;
if (count < 2)
return;
firstx = points->x;
firsty = points->y;
didline = FALSE;
while (count-- > 1) {
if (didline && (gr_mode == MWROP_XOR))
drawpoint(psd, points->x, points->y);
/* note: change to drawline*/
GdLine(psd, points[0].x, points[0].y, points[1].x, points[1].y, TRUE);
points++;
didline = TRUE;
}
if (gr_mode == MWROP_XOR) {
points--;
if (points->x == firstx && points->y == firsty)
drawpoint(psd, points->x, points->y);
}
GdFixCursor(psd);
}
#if X11POLYFILL /* improved convex polygon fill routine*/
/* *********************************************************
Copyright (c) 1987 X Consortium
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.
Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
******************************************************************/
/*
* Written by Brian Kelleher; Dec. 1985.
* Adapted for Microwindows Sep 2001 by Greg Haerr <greg@censoft.com>
*
* Fill a convex polygon in the fg color, with clipping.
* If the given polygon
* is not convex, then the result is undefined.
* The algorithm is to order the edges from smallest
* y to largest by partitioning the array into a left
* edge list and a right edge list. The algorithm used
* to traverse each edge is an extension of Bresenham's
* line algorithm with y as the major axis.
*
* This file contains a few macros to help track
* the edge of a filled object. The object is assumed
* to be filled in scanline order, and thus the
* algorithm used is an extension of Bresenham's line
* drawing algorithm which assumes that y is always the
* major axis.
*
* In scan converting polygons, we want to choose those pixels
* which are inside the polygon. Thus, we add .5 to the starting
* x coordinate for both left and right edges. Now we choose the
* first pixel which is inside the pgon for the left edge and the
* first pixel which is outside the pgon for the right edge.
* Draw the left pixel, but not the right.
*
* How to add .5 to the starting x coordinate:
* If the edge is moving to the right, then subtract dy from the
* error term from the general form of the algorithm.
* If the edge is moving to the left, then add dy to the error term.
*
* The reason for the difference between edges moving to the left
* and edges moving to the right is simple: If an edge is moving
* to the right, then we want the algorithm to flip immediately.
* If it is moving to the left, then we don't want it to flip until
* we traverse an entire pixel.
*/
#define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
int dx; /* local storage */ \
\
/* \
* if the edge is horizontal, then it is ignored \
* and assumed not to be processed. Otherwise, do this stuff. \
*/ \
if ((dy) != 0) { \
xStart = (x1); \
dx = (x2) - xStart; \
if (dx < 0) { \
m = dx / (dy); \
m1 = m - 1; \
incr1 = -2 * dx + 2 * (dy) * m1; \
incr2 = -2 * dx + 2 * (dy) * m; \
d = 2 * m * (dy) - 2 * dx - 2 * (dy); \
} else { \
m = dx / (dy); \
m1 = m + 1; \
incr1 = 2 * dx - 2 * (dy) * m1; \
incr2 = 2 * dx - 2 * (dy) * m; \
d = -2 * m * (dy) + 2 * dx; \
} \
} \
}
#define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
if (m1 > 0) { \
if (d > 0) { \
minval += m1; \
d += incr1; \
} \
else { \
minval += m; \
d += incr2; \
} \
} else {\
if (d >= 0) { \
minval += m1; \
d += incr1; \
} \
else { \
minval += m; \
d += incr2; \
} \
} \
}
/*
* Find the index of the point with the smallest y.
*/
static int
getPolyYBounds(MWPOINT *pts, int n, int *by, int *ty)
{
MWPOINT *ptMin;
int ymin, ymax;
MWPOINT *ptsStart = pts;
ptMin = pts;
ymin = ymax = (pts++)->y;
while (--n > 0) {
if (pts->y < ymin)
{
ptMin = pts;
ymin = pts->y;
}
if(pts->y > ymax)
ymax = pts->y;
pts++;
}
*by = ymin;
*ty = ymax;
return(ptMin-ptsStart);
}
static int
getPolyXBounds(MWPOINT *pts, int n, int *bx, int *tx)
{
MWPOINT *ptMin;
int xmin, xmax;
MWPOINT *ptsStart = pts;
ptMin = pts;
xmin = xmax = (pts++)->x;
while (--n > 0) {
if (pts->x < xmin)
{
ptMin = pts;
xmin = pts->x;
}
if(pts->x > xmax)
xmax = pts->x;
pts++;
}
*bx = xmin;
*tx = xmax;
return(ptMin-ptsStart);
}
void
GdFillPoly(PSD psd, int count, MWPOINT *pointtable)
{
MWCOORD xl = 0, xr = 0; /* x vals of left and right edges */
int dl = 0, dr = 0; /* decision variables */
int ml = 0, m1l = 0; /* left edge slope and slope+1 */
int mr = 0, m1r = 0; /* right edge slope and slope+1 */
int incr1l = 0, incr2l = 0; /* left edge error increments */
int incr1r = 0, incr2r = 0; /* right edge error increments */
int dy; /* delta y */
MWCOORD y; /* current scanline */
int left, right; /* indices to first endpoints */
int i; /* loop counter */
int nextleft, nextright; /* indices to second endpoints */
MWPOINT *ptsOut, *FirstPoint;/* output buffer */
MWCOORD *width, *FirstWidth;/* output buffer */
int imin; /* index of smallest vertex (in y)*/
int ymin; /* y-extents of polygon */
int ymax;
/*
* find leftx, bottomy, rightx, topy, and the index
* of bottomy.
*/
imin = getPolyYBounds(pointtable, count, &ymin, &ymax);
if (gr_fillmode != MWFILL_SOLID) {
int xmin, xmax;
getPolyXBounds(pointtable, count, &xmin, &xmax);
set_ts_origin(xmin, ymin);
}
dy = ymax - ymin + 1;
if ((count < 3) || (dy < 0))
return;
ptsOut = FirstPoint = (MWPOINT *)ALLOCA(sizeof(MWPOINT) * dy);
width = FirstWidth = (MWCOORD *)ALLOCA(sizeof(MWCOORD) * dy);
if(!FirstPoint || !FirstWidth)
{
if (FirstWidth) FREEA(FirstWidth);
if (FirstPoint) FREEA(FirstPoint);
return;
}
nextleft = nextright = imin;
y = pointtable[nextleft].y;
/*
* loop through all edges of the polygon
*/
do {
/*
* add a left edge if we need to
*/
if (pointtable[nextleft].y == y) {
left = nextleft;
/*
* find the next edge, considering the end
* conditions of the array.
*/
nextleft++;
if (nextleft >= count)
nextleft = 0;
/*
* now compute all of the random information
* needed to run the iterative algorithm.
*/
BRESINITPGON(pointtable[nextleft].y-pointtable[left].y,
pointtable[left].x,pointtable[nextleft].x,
xl, dl, ml, m1l, incr1l, incr2l);
}
/*
* add a right edge if we need to
*/
if (pointtable[nextright].y == y) {
right = nextright;
/*
* find the next edge, considering the end
* conditions of the array.
*/
nextright--;
if (nextright < 0)
nextright = count-1;
/*
* now compute all of the random information
* needed to run the iterative algorithm.
*/
BRESINITPGON(pointtable[nextright].y-pointtable[right].y,
pointtable[right].x,pointtable[nextright].x,
xr, dr, mr, m1r, incr1r, incr2r);
}
/*
* generate scans to fill while we still have
* a right edge as well as a left edge.
*/
i = MWMIN(pointtable[nextleft].y, pointtable[nextright].y) - y;
/* in case we're called with non-convex polygon */
if(i < 0)
{
FREEA(FirstWidth);
FREEA(FirstPoint);
return;
}
while (i-- > 0)
{
ptsOut->y = y;
/*
* reverse the edges if necessary
*/
if (xl < xr)
{
*(width++) = xr - xl;
(ptsOut++)->x = xl;
}
else
{
*(width++) = xl - xr;
(ptsOut++)->x = xr;
}
y++;
/* increment down the edges */
BRESINCRPGON(dl, xl, ml, m1l, incr1l, incr2l);
BRESINCRPGON(dr, xr, mr, m1r, incr1r, incr2r);
}
} while (y != ymax);
/*
* Finally, fill the spans
*/
i = ptsOut-FirstPoint;
ptsOut = FirstPoint;
width = FirstWidth;
while (--i >= 0) {
/* calc x extent from width*/
int e = *width++ - 1;
if (e >= 0) {
if (gr_fillmode != MWFILL_SOLID)
ts_drawrow(psd, ptsOut->x, ptsOut->x + e, ptsOut->y);
else
drawrow(psd, ptsOut->x, ptsOut->x + e, ptsOut->y);
}
++ptsOut;
}
FREEA(FirstWidth);
FREEA(FirstPoint);
GdFixCursor(psd);
}
#endif /* X11POLYFILL*/
#if BASICPOLYFILL /* original convex only polygon fill routine*/
/*
* Fill a polygon in the foreground color, applying clipping if necessary.
* The last point may be a duplicate of the first point, but this is
* not required.
* Note: this routine currently only correctly fills convex polygons.
*/
/* Utility routine for filling polygons. Find the intersection point (if
* any) of a horizontal line with an arbitrary line, and extend the current
* minimum and maximum x values as needed to include the intersection point.
* Input parms:
* y row to check for intersection
* x1, y1 first endpoint
* x2, y2 second enpoint
* minxptr address of current minimum x
* maxxptr address of current maximum x
*/
static void
extendrow(MWCOORD y,MWCOORD x1,MWCOORD y1,MWCOORD x2,MWCOORD y2,
MWCOORD *minxptr,MWCOORD *maxxptr)
{
MWCOORD x; /* x coordinate of intersection */
typedef long NUM;
NUM num; /* numerator of fraction */
/* First make sure the specified line segment includes the specified
* row number. If not, then there is no intersection.
*/
if (((y < y1) || (y > y2)) && ((y < y2) || (y > y1)))
return;
/* If a horizontal line, then check the two endpoints. */
if (y1 == y2) {
if (*minxptr > x1) *minxptr = x1;
if (*minxptr > x2) *minxptr = x2;
if (*maxxptr < x1) *maxxptr = x1;
if (*maxxptr < x2) *maxxptr = x2;
return;
}
/* If a vertical line, then check the x coordinate. */
if (x1 == x2) {
if (*minxptr > x1) *minxptr = x1;
if (*maxxptr < x1) *maxxptr = x1;
return;
}
/* An arbitrary line. Calculate the intersection point using the
* formula x = x1 + (y - y1) * (x2 - x1) / (y2 - y1).
*/
num = ((NUM) (y - y1)) * (x2 - x1);
x = x1 + num / (y2 - y1);
if (*minxptr > x) *minxptr = x;
if (*maxxptr < x) *maxxptr = x;
}
void
GdFillPoly(PSD psd, int count, MWPOINT *points)
{
MWPOINT *pp; /* current point */
MWCOORD miny; /* minimum row */
MWCOORD maxy; /* maximum row */
MWCOORD minx; /* minimum column */
MWCOORD maxx; /* maximum column */
int i; /* counter */
if (count <= 0)
return;
/* First determine the minimum and maximum rows for the polygon. */
pp = points;
miny = pp->y;
maxy = pp->y;
for (i = count; i-- > 0; pp++) {
if (miny > pp->y) miny = pp->y;
if (maxy < pp->y) maxy = pp->y;
}
if (miny < 0)
miny = 0;
if (maxy >= psd->yvirtres)
maxy = psd->yvirtres - 1;
if (miny > maxy)
return;
/* Now for each row, scan the list of points and determine the
* minimum and maximum x coordinate for each line, and plot the row.
* The last point connects with the first point automatically.
*/
for (; miny <= maxy; miny++) {
minx = MAX_MWCOORD;
maxx = MIN_MWCOORD;
pp = points;
for (i = count; --i > 0; pp++)
extendrow(miny, pp[0].x, pp[0].y, pp[1].x, pp[1].y,
&minx, &maxx);
extendrow(miny, pp[0].x, pp[0].y, points[0].x, points[0].y,
&minx, &maxx);
if (minx <= maxx)
drawrow(psd, minx, maxx, miny);
}
GdFixCursor(psd);
}
#endif /* BASICPOLYFILL*/
#if EDGEPOLYFILL /* irregular polygon fill, uses edge table, malloc, qsort*/
/*
* Fill a polygon in the foreground color, applying clipping if necessary.
* The last point may be a duplicate of the first point, but this is
* not required.
* Note: this routine correctly draws convex, concave, regular,
* and irregular polygons.
*/
#define swap(a,b) do { a ^= b; b ^= a; a ^= b; } while (0)
typedef struct {
int x1, y1, x2, y2;
#if HAVE_FLOAT
double x, m;
#else
int cx, fn, mn, d;
#endif
} edge_t;
static int
edge_cmp(const void *lvp, const void *rvp)
{
/* convert from void pointers to structure pointers */
const edge_t *lp = (const edge_t *)lvp;
const edge_t *rp = (const edge_t *)rvp;
/* if the minimum y values are different, sort on minimum y */
if (lp->y1 != rp->y1)
return lp->y1 - rp->y1;
/* if the current x values are different, sort on current x */
#if HAVE_FLOAT
if (lp->x < rp->x)
return -1;
else if (lp->x > rp->x)
return +1;
#else
if (lp->cx != rp->cx)
return lp->cx - rp->cx;
#endif
/* otherwise they are equal */
return 0;
}
/**
* Draw a filled polygon.
*
* @param psd Drawing surface.
* @param count Number of points in polygon.
* @param pointtable The array of points.
*/
void
GdFillPoly(PSD psd, int count, MWPOINT * pointtable)
{
edge_t *get; /* global edge table */
int nge = 0; /* num global edges */
int cge = 0; /* cur global edge */
edge_t *aet; /* active edge table */
int nae = 0; /* num active edges */
int i, y;
if (count < 3) {
/* error, polygons require at least three edges (a triangle) */
return;
}
get = (edge_t *) calloc(count, sizeof(edge_t));
aet = (edge_t *) calloc(count, sizeof(edge_t));
if ((get == 0) || (aet == 0)) {
/* error, couldn't allocate one or both of the needed tables */
if (get)
free(get);
if (aet)
free(aet);
return;
}
/* setup the global edge table */
for (i = 0; i < count; ++i) {
get[nge].x1 = pointtable[i].x;
get[nge].y1 = pointtable[i].y;
get[nge].x2 = pointtable[(i + 1) % count].x;
get[nge].y2 = pointtable[(i + 1) % count].y;
if (get[nge].y1 != get[nge].y2) {
if (get[nge].y1 > get[nge].y2) {
swap(get[nge].x1, get[nge].x2);
swap(get[nge].y1, get[nge].y2);
}
#if HAVE_FLOAT
get[nge].x = get[nge].x1;
get[nge].m = get[nge].x2 - get[nge].x1;
get[nge].m /= get[nge].y2 - get[nge].y1;
#else
get[nge].cx = get[nge].x1;
get[nge].mn = get[nge].x2 - get[nge].x1;
get[nge].d = get[nge].y2 - get[nge].y1;
get[nge].fn = get[nge].mn / 2;
#endif
++nge;
}
}
qsort(get, nge, sizeof(get[0]), edge_cmp);
/* start with the lowest y in the table */
y = get[0].y1;
do {
/* add edges to the active table from the global table */
while ((nge > 0) && (get[cge].y1 == y)) {
aet[nae] = get[cge++];
--nge;
aet[nae++].y1 = 0;
}
qsort(aet, nae, sizeof(aet[0]), edge_cmp);
/* using odd parity, render alternating line segments */
for (i = 1; i < nae; i += 2) {
#if HAVE_FLOAT
int l = (int)aet[i - 1].x;
int r = (int)aet[i].x;
#else
int l = (int)aet[i - 1].cx;
int r = (int)aet[i].cx;
#endif
if (r > l)
drawrow(psd, l, r, y); /* draw line between l and r and not between l and (r-1) */
}
/* prepare for the next scan line */
++y;
/* remove inactive edges from the active edge table */
/* or update the current x position of active edges */
for (i = 0; i < nae; ++i) {
if (aet[i].y2 == y)
aet[i--] = aet[--nae];
else {
#if HAVE_FLOAT
aet[i].x += aet[i].m;
#else
aet[i].fn += aet[i].mn;
if (aet[i].fn < 0) {
aet[i].cx += aet[i].fn / aet[i].d - 1;
aet[i].fn %= aet[i].d;
aet[i].fn += aet[i].d;
}
if (aet[i].fn >= aet[i].d) {
aet[i].cx += aet[i].fn / aet[i].d;
aet[i].fn %= aet[i].d;
}
#endif
}
}
/* keep doing this while there are any edges left */
} while ((nae > 0) || (nge > 0));
/* all done, free the edge tables */
free(get);
free(aet);
GdFixCursor(psd);
}
#endif /* EDGEPOLYFILL*/