home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / video / 6 / 6_11.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  2.3 KB  |  126 lines

  1. /* Listing 6-11 */
  2.  
  3. struct    EndpointStruct        /* endpoints for clipped line */
  4. {
  5.   int    x1,y1;
  6.   int    x2,y2;
  7. };
  8.  
  9. struct    RegionStruct        /* rectangular clipping region */
  10. {
  11.   int    Xul;
  12.   int    Yul;
  13.   int    Xlr;
  14.   int    Ylr;
  15. };
  16.  
  17.  
  18. union OutcodeUnion    /* outcodes are represented as bit fields */
  19. {
  20.   struct
  21.   {
  22.     unsigned code0 : 1;        /* x < Xul */
  23.     unsigned code1 : 1;     /* y < Yul */
  24.     unsigned code2 : 1;     /* x > Xlr */
  25.     unsigned code3 : 1;     /* y > Ylr */
  26.   }
  27.     ocs;
  28.  
  29.   int    outcodes;
  30. };
  31.  
  32.  
  33. #define    X1    ep->x1
  34. #define    Y1    ep->y1
  35. #define    X2    ep->x2
  36. #define    Y2    ep->y2
  37. #define    XUL    r->Xul
  38. #define    YUL    r->Yul
  39. #define    XLR    r->Xlr
  40. #define    YLR    r->Ylr
  41.  
  42.  
  43. Clip(ep,r)
  44. struct    EndpointStruct    *ep;
  45. struct    RegionStruct    *r;
  46. {
  47.     union OutcodeUnion    ocu1,ocu2;
  48.     int     Inside;
  49.     int    Outside;
  50.  
  51.  
  52.     /* initialize 4-bit codes */
  53.  
  54.     SetOutcodes( &ocu1, r, X1, Y1 );    /* initial 4-bit codes */
  55.     SetOutcodes( &ocu2, r, X2, Y2 );
  56.  
  57.     Inside  = ((ocu1.outcodes | ocu2.outcodes) == 0);
  58.     Outside = ((ocu1.outcodes & ocu2.outcodes) != 0);
  59.  
  60.     while (!Outside && !Inside)
  61.     {
  62.       if (ocu1.outcodes==0)        /* swap endpoints if necessary so */
  63.       {                /*  that (x1,y1) needs to be clipped */
  64.         Swap( &X1, &X2 );
  65.         Swap( &Y1, &Y2 );
  66.         Swap( &ocu1, &ocu2 );
  67.       }
  68.  
  69.  
  70.       if (ocu1.ocs.code0)            /* clip left */
  71.       {
  72.         Y1 += (long)(Y2-Y1)*(XUL-X1)/(X2-X1);
  73.         X1 = XUL;
  74.       }
  75.  
  76.       else if (ocu1.ocs.code1)        /* clip above */
  77.       {
  78.         X1 += (long)(X2-X1)*(YUL-Y1)/(Y2-Y1);
  79.         Y1 = YUL;
  80.       }
  81.  
  82.       else if (ocu1.ocs.code2)        /* clip right */
  83.       {
  84.         Y1 += (long)(Y2-Y1)*(XLR-X1)/(X2-X1);
  85.         X1 = XLR;
  86.       }
  87.  
  88.       else if (ocu1.ocs.code3)        /* clip below */
  89.       {
  90.         X1 += (long)(X2-X1)*(YLR-Y1)/(Y2-Y1);
  91.         Y1 = YLR;
  92.       }
  93.  
  94.           SetOutcodes( &ocu1, r, X1, Y1 );          /* update for (x1,y1) */
  95.  
  96.       Inside  = ((ocu1.outcodes | ocu2.outcodes) == 0); /* update       */
  97.       Outside = ((ocu1.outcodes & ocu2.outcodes) != 0); /*  4-bit codes */
  98.     }
  99.  
  100.     return( Inside );
  101. }
  102.  
  103.     
  104. SetOutcodes( u, r, x, y )
  105. union OutcodeUnion    *u;
  106. struct RegionStruct    *r;
  107. int    x,y;
  108. {
  109.     u->outcodes = 0;
  110.     u->ocs.code0 = (x < XUL);
  111.     u->ocs.code1 = (y < YUL);
  112.     u->ocs.code2 = (x > XLR);
  113.     u->ocs.code3 = (y > YLR);
  114. }
  115.  
  116.     
  117. Swap( pa, pb )
  118. int    *pa,*pb;
  119. {
  120.     int    t;
  121.  
  122.     t = *pa;
  123.     *pa = *pb;
  124.     *pb = t;
  125. }
  126.