home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter39 / l39-4.c < prev    next >
C/C++ Source or Header  |  1997-06-18  |  2KB  |  39 lines

  1. /* Scan converts an edge from (X1,Y1) to (X2,Y2), not including the
  2.    point at (X2,Y2). This avoids overlapping the end of one line with
  3.    the start of the next, and causes the bottom scan line of the
  4.    polygon not to be drawn. If SkipFirst != 0, the point at (X1,Y1)
  5.    isn't drawn. For each scan line, the pixel closest to the scanned
  6.    line without being to the left of the scanned line is chosen */
  7.  
  8. #include <math.h>
  9. #include "polygon.h"
  10.  
  11. void ScanEdge(int X1, int Y1, int X2, int Y2, int SetXStart,
  12.       int SkipFirst, struct HLine **EdgePointPtr)
  13. {
  14.    int Y, DeltaX, DeltaY;
  15.    double InverseSlope;
  16.    struct HLine *WorkingEdgePointPtr;
  17.  
  18.    /* Calculate X and Y lengths of the line and the inverse slope */
  19.    DeltaX = X2 - X1;
  20.    if ((DeltaY = Y2 - Y1) <= 0)
  21.       return;     /* guard against 0-length and horizontal edges */
  22.    InverseSlope = (double)DeltaX / (double)DeltaY;
  23.  
  24.    /* Store the X coordinate of the pixel closest to but not to the
  25.       left of the line for each Y coordinate between Y1 and Y2, not
  26.       including Y2 and also not including Y1 if SkipFirst != 0 */
  27.    WorkingEdgePointPtr = *EdgePointPtr; /* avoid double dereference */
  28.    for (Y = Y1 + SkipFirst; Y < Y2; Y++, WorkingEdgePointPtr++) {
  29.       /* Store the X coordinate in the appropriate edge list */
  30.       if (SetXStart == 1)
  31.          WorkingEdgePointPtr->XStart =
  32.                X1 + (int)(ceil((Y-Y1) * InverseSlope));
  33.       else
  34.          WorkingEdgePointPtr->XEnd =
  35.                X1 + (int)(ceil((Y-Y1) * InverseSlope));
  36.    }
  37.    *EdgePointPtr = WorkingEdgePointPtr;   /* advance caller's ptr */
  38. }
  39.