/********************************************************************** * gcc -g -o fill fill.c -lm -lXm -lXt -lX11 * * fill 3 90 50 110 120 130 50 3 290 50 310 120 330 50 4 210 110 190 160 210 180 230 160 4 10 170 210 350 410 170 210 280 **********************************************************************/ #include #include #include #include typedef struct edge { int yUpper; float xLower; float invSlope; struct edge *next; } Edge; typedef struct point { int x; int y; } Point; int numPolys; int numVertices[100]; Point coords[1000]; void fill(Widget w, GC gc, Point coords[], int numV[], int numPolys); void buildET(Point *coords, int numV, Edge *ET[], int sizeET); void sort(Edge *AET); void printList(Edge *h, char *str); void fillspan(Widget w, GC gc, int x1, int y1, int x2, int y2); int round(float x) { return (int)(x+.5);}; void redraw(Widget w, XtPointer client_data, XtPointer call_data); /**********************************************************************/ int main(int argc, char *argv[]) { XtAppContext app_context; Widget toplevel, canvas; GC gc; XColor color; XGCValues values; Arg wargs[2]; int n; int firstv,i; /* Create the top level window shell */ toplevel = XtAppInitialize(&app_context,argv[0], NULL, 0, &argc, argv, NULL, NULL, 0); /* Create the drawing area widget within the toplevel shell. */ n = 0; XtSetArg(wargs[n], XmNwidth, 500); n++; XtSetArg(wargs[n], XmNheight, 500); n++; canvas = XtCreateManagedWidget("canvas", xmDrawingAreaWidgetClass, toplevel,wargs,n); /* * Create a gc to draw with. Make sure it's foreground color is the * default foreground color of the widget, so it can be changed on the * command line. */ n = 0; XtSetArg(wargs[n], XmNforeground, &(color.pixel)); n++; XtGetValues(canvas, wargs, n); values.foreground = color.pixel; gc = XCreateGC(XtDisplay(canvas), DefaultRootWindow(XtDisplay(canvas)), GCForeground, &values); /* Tell X the name of the function that redraws the picture. */ XtAddCallback(canvas, XmNexposeCallback, redraw, gc); /* Make the widget real. */ XtRealizeWidget(toplevel); /* Read in all coordinates */ numPolys = 0; firstv = 0; while (scanf("%d",&numVertices[numPolys]) != EOF) { for (i=0; inext = NULL; /* Find first row for which there is a nonempty element in ET. */ /* Repeat for each scan line row as long as AET still has something */ /* and we are at less than the 2,000th row. */ /* Insert all new edges into the AET. */ /* Sort all active edges by xLower */ /* Fill the spans between pairs in the AET */ /* Remove edges whose yUpper is at this row */ /* Update scan row and all active edge xLower's */ } } /********************************************************************** * buildET: Construct the initial Edge Table for one polygon. **********************************************************************/ void buildET(Point *coords, int numV, Edge *ET[], int sizeET) { /* Set all ET entries to NULL. */ /* Repeat for each edge */ /* If not a horizontal line... */ /* Determine low and high endpoint. */ /* Allocate memory for new Edge. Assign invSlope, xLower, yUpper*/ /* Decrease yUpper by one if not a local extremum. */ /* Insert the edge into ET */ } /********************************************************************** * fillspan: Actually draw the pixels. **********************************************************************/ void fillspan(Widget w,GC gc,int x1, int y1, int x2, int y2) { /* Call XDrawLine to fill the span. */ } /********************************************************************** * sort: Use bubble sort algorithm to sort the linked list of nodes of * type Edge. Sort in increasing xLower values. Assumes AET starts with * a dummy node. **********************************************************************/ void sort(Edge *AET) { Edge *p, *q, *r; int anyswaps=1; while (anyswaps) { anyswaps = 0; p = AET; q = p->next; if (q == NULL) return; r = q->next; printList(AET->next,"Before sorting "); while (r != NULL) { printList(AET->next," in sort loop "); printf(" Comparing %f and %f\n",q->xLower,r->xLower); if (q->xLower > r->xLower) { printf(" Swapping them\n"); anyswaps = 1; q->next = r->next; r->next = q; p->next = r; } p = p->next; q = p->next; r = q->next; } } printList(AET->next," Sorted "); } /********************************************************************** * printList: Print the list of Edges **********************************************************************/ void printList(Edge *h, char *str) { Edge *p; printf("%s",str); for (p=h; p!=NULL; p=p->next) printf(" (%d %.2f %.2f)",p->yUpper,p->xLower,p->invSlope); printf("\n"); }