_GRAPHICS PROGRAMMING COLUMN_ by Michael Abrash [LISTING ONE] /* Color-fills a convex polygon. All vertices are offset by (XOffset, YOffset). "Convex" means that every horizontal line drawn through the polygon at any point would cross exactly two active edges (neither horizontal lines nor zero-length edges count as active edges; both are acceptable anywhere in the polygon), and that the right & left edges never cross. (It's OK for them to touch, though, so long as the right edge never crosses over to the left of the left edge.) Nonconvex polygons won't be drawn properly. Returns 1 for success, 0 if memory allocation failed */ #include #include #ifdef __TURBOC__ #include #else /* MSC */ #include #endif #include "polygon.h" /* Advances the index by one vertex forward through the vertex list, wrapping at the end of the list */ #define INDEX_FORWARD(Index) \ Index = (Index + 1) % VertexList->Length; /* Advances the index by one vertex backward through the vertex list, wrapping at the start of the list */ #define INDEX_BACKWARD(Index) \ Index = (Index - 1 + VertexList->Length) % VertexList->Length; /* Advances the index by one vertex either forward or backward through the vertex list, wrapping at either end of the list */ #define INDEX_MOVE(Index,Direction) \ if (Direction > 0) \ Index = (Index + 1) % VertexList->Length; \ else \ Index = (Index - 1 + VertexList->Length) % VertexList->Length; extern void DrawHorizontalLineList(struct HLineList *, int); static void ScanEdge(int, int, int, int, int, int, struct HLine **); int FillConvexPolygon(struct PointListHeader * VertexList, int Color, int XOffset, int YOffset) { int i, MinIndexL, MaxIndex, MinIndexR, SkipFirst, Temp; int MinPoint_Y, MaxPoint_Y, TopIsFlat, LeftEdgeDir; int NextIndex, CurrentIndex, PreviousIndex; int DeltaXN, DeltaYN, DeltaXP, DeltaYP; struct HLineList WorkingHLineList; struct HLine *EdgePointPtr; struct Point *VertexPtr; /* Point to the vertex list */ VertexPtr = VertexList->PointPtr; /* Scan the list to find the top and bottom of the polygon */ if (VertexList->Length == 0) return(1); /* reject null polygons */ MaxPoint_Y = MinPoint_Y = VertexPtr[MinIndexL = MaxIndex = 0].Y; for (i = 1; i < VertexList->Length; i++) { if (VertexPtr[i].Y < MinPoint_Y) MinPoint_Y = VertexPtr[MinIndexL = i].Y; /* new top */ else if (VertexPtr[i].Y > MaxPoint_Y) MaxPoint_Y = VertexPtr[MaxIndex = i].Y; /* new bottom */ } if (MinPoint_Y == MaxPoint_Y) return(1); /* polygon is 0-height; avoid infinite loop below */ /* Scan in ascending order to find the last top-edge point */ MinIndexR = MinIndexL; while (VertexPtr[MinIndexR].Y == MinPoint_Y) INDEX_FORWARD(MinIndexR); INDEX_BACKWARD(MinIndexR); /* back up to last top-edge point */ /* Now scan in descending order to find the first top-edge point */ while (VertexPtr[MinIndexL].Y == MinPoint_Y) INDEX_BACKWARD(MinIndexL); INDEX_FORWARD(MinIndexL); /* back up to first top-edge point */ /* Figure out which direction through the vertex list from the top vertex is the left edge and which is the right */ LeftEdgeDir = -1; /* assume left edge runs down thru vertex list */ if ((TopIsFlat = (VertexPtr[MinIndexL].X != VertexPtr[MinIndexR].X) ? 1 : 0) == 1) { /* If the top is flat, just see which of the ends is leftmost */ if (VertexPtr[MinIndexL].X > VertexPtr[MinIndexR].X) { LeftEdgeDir = 1; /* left edge runs up through vertex list */ Temp = MinIndexL; /* swap the indices so MinIndexL */ MinIndexL = MinIndexR; /* points to the start of the left */ MinIndexR = Temp; /* edge, similarly for MinIndexR */ } } else { /* Point to the downward end of the first line of each of the two edges down from the top */ NextIndex = MinIndexR; INDEX_FORWARD(NextIndex); PreviousIndex = MinIndexL; INDEX_BACKWARD(PreviousIndex); /* Calculate X and Y lengths from the top vertex to the end of the first line down each edge; use those to compare slopes and see which line is leftmost */ DeltaXN = VertexPtr[NextIndex].X - VertexPtr[MinIndexL].X; DeltaYN = VertexPtr[NextIndex].Y - VertexPtr[MinIndexL].Y; DeltaXP = VertexPtr[PreviousIndex].X - VertexPtr[MinIndexL].X; DeltaYP = VertexPtr[PreviousIndex].Y - VertexPtr[MinIndexL].Y; if (((long)DeltaXN * DeltaYP - (long)DeltaYN * DeltaXP) < 0L) { LeftEdgeDir = 1; /* left edge runs up through vertex list */ Temp = MinIndexL; /* swap the indices so MinIndexL */ MinIndexL = MinIndexR; /* points to the start of the left */ MinIndexR = Temp; /* edge, similarly for MinIndexR */ } } /* Set the # of scan lines in the polygon, skipping the bottom edge and also skipping the top vertex if the top isn't flat because in that case the top vertex has a right edge component, and set the top scan line to draw, which is likewise the second line of the polygon unless the top is flat */ if ((WorkingHLineList.Length = MaxPoint_Y - MinPoint_Y - 1 + TopIsFlat) <= 0) return(1); /* there's nothing to draw, so we're done */ WorkingHLineList.YStart = YOffset + MinPoint_Y + 1 - TopIsFlat; /* Get memory in which to store the line list we generate */ if ((WorkingHLineList.HLinePtr = (struct HLine *) (malloc(sizeof(struct HLine) * WorkingHLineList.Length))) == NULL) return(0); /* couldn't get memory for the line list */ /* Scan the left edge and store the boundary points in the list */ /* Initial pointer for storing scan converted left-edge coords */ EdgePointPtr = WorkingHLineList.HLinePtr; /* Start from the top of the left edge */ PreviousIndex = CurrentIndex = MinIndexL; /* Skip the first point of the first line unless the top is flat; if the top isn't flat, the top vertex is exactly on a right edge and isn't drawn */ SkipFirst = TopIsFlat ? 0 : 1; /* Scan convert each line in the left edge from top to bottom */ do { INDEX_MOVE(CurrentIndex,LeftEdgeDir); ScanEdge(VertexPtr[PreviousIndex].X + XOffset, VertexPtr[PreviousIndex].Y, VertexPtr[CurrentIndex].X + XOffset, VertexPtr[CurrentIndex].Y, 1, SkipFirst, &EdgePointPtr); PreviousIndex = CurrentIndex; SkipFirst = 0; /* scan convert the first point from now on */ } while (CurrentIndex != MaxIndex); /* Scan the right edge and store the boundary points in the list */ EdgePointPtr = WorkingHLineList.HLinePtr; PreviousIndex = CurrentIndex = MinIndexR; SkipFirst = TopIsFlat ? 0 : 1; /* Scan convert the right edge, top to bottom. X coordinates are adjusted 1 to the left, effectively causing scan conversion of the nearest points to the left of but not exactly on the edge */ do { INDEX_MOVE(CurrentIndex,-LeftEdgeDir); ScanEdge(VertexPtr[PreviousIndex].X + XOffset - 1, VertexPtr[PreviousIndex].Y, VertexPtr[CurrentIndex].X + XOffset - 1, VertexPtr[CurrentIndex].Y, 0, SkipFirst, &EdgePointPtr); PreviousIndex = CurrentIndex; SkipFirst = 0; /* scan convert the first point from now on */ } while (CurrentIndex != MaxIndex); /* Draw the line list representing the scan converted polygon */ DrawHorizontalLineList(&WorkingHLineList, Color); /* Release the line list's memory and we're successfully done */ free(WorkingHLineList.HLinePtr); return(1); } /* Scan converts an edge from (X1,Y1) to (X2,Y2), not including the point at (X2,Y2). This avoids overlapping the end of one line with the start of the next, and causes the bottom scan line of the polygon not to be drawn. If SkipFirst != 0, the point at (X1,Y1) isn't drawn. For each scan line, the pixel closest to the scanned line without being to the left of the scanned line is chosen */ static void ScanEdge(int X1, int Y1, int X2, int Y2, int SetXStart, int SkipFirst, struct HLine **EdgePointPtr) { int Y, DeltaX, DeltaY; double InverseSlope; struct HLine *WorkingEdgePointPtr; /* Calculate X and Y lengths of the line and the inverse slope */ DeltaX = X2 - X1; if ((DeltaY = Y2 - Y1) <= 0) return; /* guard against 0-length and horizontal edges */ InverseSlope = (double)DeltaX / (double)DeltaY; /* Store the X coordinate of the pixel closest to but not to the left of the line for each Y coordinate between Y1 and Y2, not including Y2 and also not including Y1 if SkipFirst != 0 */ WorkingEdgePointPtr = *EdgePointPtr; /* avoid double dereference */ for (Y = Y1 + SkipFirst; Y < Y2; Y++, WorkingEdgePointPtr++) { /* Store the X coordinate in the appropriate edge list */ if (SetXStart == 1) WorkingEdgePointPtr->XStart = X1 + (int)(ceil((Y-Y1) * InverseSlope)); else WorkingEdgePointPtr->XEnd = X1 + (int)(ceil((Y-Y1) * InverseSlope)); } *EdgePointPtr = WorkingEdgePointPtr; /* advance caller's ptr */ } [LISTING TWO] /* Draws all pixels in the list of horizontal lines passed in, in mode 13h, the VGA's 320x200 256-color mode. Uses a slow pixel-by- pixel approach, which does have the virtue of being easily ported to any environment. */ #include #include "polygon.h" #define SCREEN_WIDTH 320 #define SCREEN_SEGMENT 0xA000 static void DrawPixel(int, int, int); void DrawHorizontalLineList(struct HLineList * HLineListPtr, int Color) { struct HLine *HLinePtr; int Y, X; /* Point to the XStart/XEnd descriptor for the first (top) horizontal line */ HLinePtr = HLineListPtr->HLinePtr; /* Draw each horizontal line in turn, starting with the top one and advancing one line each time */ for (Y = HLineListPtr->YStart; Y < (HLineListPtr->YStart + HLineListPtr->Length); Y++, HLinePtr++) { /* Draw each pixel in the current horizontal line in turn, starting with the leftmost one */ for (X = HLinePtr->XStart; X <= HLinePtr->XEnd; X++) DrawPixel(X, Y, Color); } } /* Draws the pixel at (X, Y) in color Color in VGA mode 13h */ static void DrawPixel(int X, int Y, int Color) { unsigned char far *ScreenPtr; #ifdef __TURBOC__ ScreenPtr = MK_FP(SCREEN_SEGMENT, Y * SCREEN_WIDTH + X); #else /* MSC 5.0 */ FP_SEG(ScreenPtr) = SCREEN_SEGMENT; FP_OFF(ScreenPtr) = Y * SCREEN_WIDTH + X; #endif *ScreenPtr = (unsigned char)Color; } [LISTING THREE] /* Sample program to exercise the polygon-filling routines. This code and all polygon-filling code has been tested with Turbo C 2.0 and Microsoft C 5.0 */ #include #include #include "polygon.h" /* Draws the polygon described by the point list PointList in color Color with all vertices offset by (X,Y) */ #define DRAW_POLYGON(PointList,Color,X,Y) \ Polygon.Length = sizeof(PointList)/sizeof(struct Point); \ Polygon.PointPtr = PointList; \ FillConvexPolygon(&Polygon, Color, X, Y); void main(void); extern int FillConvexPolygon(struct PointListHeader *, int, int, int); void main() { int i, j; struct PointListHeader Polygon; static struct Point ScreenRectangle[] = {{0,0},{320,0},{320,200},{0,200}}; static struct Point ConvexShape[] = {{0,0},{121,0},{320,0},{200,51},{301,51},{250,51},{319,143}, {320,200},{22,200},{0,200},{50,180},{20,160},{50,140}, {20,120},{50,100},{20,80},{50,60},{20,40},{50,20}}; static struct Point Hexagon[] = {{90,-50},{0,-90},{-90,-50},{-90,50},{0,90},{90,50}}; static struct Point Triangle1[] = {{30,0},{15,20},{0,0}}; static struct Point Triangle2[] = {{30,20},{15,0},{0,20}}; static struct Point Triangle3[] = {{0,20},{20,10},{0,0}}; static struct Point Triangle4[] = {{20,20},{20,0},{0,10}}; union REGS regset; /* Set the display to VGA mode 13h, 320x200 256-color mode */ regset.x.ax = 0x0013; /* AH = 0 selects mode set function, AL = 0x13 selects mode 0x13 when set as parameters for INT 0x10 */ int86(0x10, ®set, ®set); /* Clear the screen to cyan */ DRAW_POLYGON(ScreenRectangle, 3, 0, 0); /* Draw an irregular shape that meets our definition of convex but is not convex by any normal description */ DRAW_POLYGON(ConvexShape, 6, 0, 0); getch(); /* wait for a keypress */ /* Draw adjacent triangles across the top half of the screen */ for (j=0; j<=80; j+=20) { for (i=0; i<290; i += 30) { DRAW_POLYGON(Triangle1, 2, i, j); DRAW_POLYGON(Triangle2, 4, i+15, j); } } /* Draw adjacent triangles across the bottom half of the screen */ for (j=100; j<=170; j+=20) { /* Do a row of pointing-right triangles */ for (i=0; i<290; i += 20) { DRAW_POLYGON(Triangle3, 40, i, j); } /* Do a row of pointing-left triangles halfway between one row of pointing-right triangles and the next, to fit between */ for (i=0; i<290; i += 20) { DRAW_POLYGON(Triangle4, 1, i, j+10); } } getch(); /* wait for a keypress */ /* Finally, draw a series of concentric hexagons of approximately the same proportions in the center of the screen */ for (i=0; i<16; i++) { DRAW_POLYGON(Hexagon, i, 160, 100); for (j=0; j= 0 ? 3 : -3; Hexagon[j].Y -= Hexagon[j].Y >= 0 ? 2 : -2; } else { Hexagon[j].Y -= Hexagon[j].Y >= 0 ? 3 : -3; } } } getch(); /* wait for a keypress */ /* Return to text mode and exit */ regset.x.ax = 0x0003; /* AL = 3 selects 80x25 text mode */ int86(0x10, ®set, ®set); } [LISTING FOUR] /* POLYGON.H: Header file for polygon-filling code */ /* Describes a single point (used for a single vertex) */ struct Point { int X; /* X coordinate */ int Y; /* Y coordinate */ }; /* Describes a series of points (used to store a list of vertices that describe a polygon; each vertex is assumed to connect to the two adjacent vertices, and the last vertex is assumed to connect to the first) */ struct PointListHeader { int Length; /* # of points */ struct Point * PointPtr; /* pointer to list of points */ }; /* Describes the beginning and ending X coordinates of a single horizontal line */ struct HLine { int XStart; /* X coordinate of leftmost pixel in line */ int XEnd; /* X coordinate of rightmost pixel in line */ }; /* Describes a Length-long series of horizontal lines, all assumed to be on contiguous scan lines starting at YStart and proceeding downward (used to describe a scan-converted polygon to the low-level hardware-dependent drawing code) */ struct HLineList { int Length; /* # of horizontal lines */ int YStart; /* Y coordinate of topmost line */ struct HLine * HLinePtr; /* pointer to list of horz lines */ };