/* Function returns TRUE if the trapezoid lies inside the polygon */ static int inside_polygon (trap_t *t, segment_t* seg) { int rseg = t->rseg; if (t->state == ST_INVALID) return 0; if ((t->lseg <= 0) || (t->rseg <= 0)) return 0; if (((t->u0 <= 0) && (t->u1 <= 0)) || ((t->d0 <= 0) && (t->d1 <= 0))) /* triangle */ return (_greater_than(&seg[rseg].v1, &seg[rseg].v0)); return 0; }
int triangulate_monotone_polygons( int nvert, int nmonpoly, int op[][3]) { register int i; point_t ymax, ymin; int p, vfirst, posmax, posmin, v; int vcount, processed; #ifdef DEBUG for (i = 0; i < nmonpoly; i++) { fprintf(stderr, "\n\nPolygon %d: ", i); vfirst = mchain[mon[i]].vnum; p = mchain[mon[i]].next; fprintf (stderr, "%d ", mchain[mon[i]].vnum); while (mchain[p].vnum != vfirst) { fprintf(stderr, "%d ", mchain[p].vnum); p = mchain[p].next; } } fprintf(stderr, "\n"); #endif op_idx = 0; for (i = 0; i < nmonpoly; i++) { vcount = 1; processed = FALSE; vfirst = mchain[mon[i]].vnum; ymax = ymin = vert[vfirst].pt; posmax = posmin = mon[i]; mchain[mon[i]].marked = TRUE; p = mchain[mon[i]].next; while ((v = mchain[p].vnum) != vfirst) { if (mchain[p].marked) { processed = TRUE; break; /* break from while */ } else mchain[p].marked = TRUE; if (_greater_than(&vert[v].pt, &ymax)) { ymax = vert[v].pt; posmax = p; } if (_less_than(&vert[v].pt, &ymin)) { ymin = vert[v].pt; posmin = p; } p = mchain[p].next; vcount++; } if (processed) /* Go to next polygon */ continue; if (vcount == 3) /* already a triangle */ { op[op_idx][0] = mchain[p].vnum; op[op_idx][1] = mchain[mchain[p].next].vnum; op[op_idx][2] = mchain[mchain[p].prev].vnum; op_idx++; } else /* triangulate the polygon */ { v = mchain[mchain[posmax].next].vnum; if (_equal_to(&vert[v].pt, &ymin)) { /* LHS is a single line */ triangulate_single_polygon(nvert, posmax, TRI_LHS, op); } else triangulate_single_polygon(nvert, posmax, TRI_RHS, op); } } #ifdef DEBUG for (i = 0; i < op_idx; i++) fprintf(stderr, "tri #%d: (%d, %d, %d)\n", i, op[i][0], op[i][1], op[i][2]); #endif return op_idx; }