home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / CSAPE32.ARJ / SOURCE / OWLSCR / OBOXCLIP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-28  |  5.4 KB  |  168 lines

  1. /*
  2.     oboxclip.c    11/2/88
  3.  
  4.     % Box related clipping functions.
  5.     Extracted from obox.c
  6.     by Ted.
  7.  
  8.     OWL 1.2
  9.     Copyright (c) 1988, by Oakland Group, Inc.
  10.     ALL RIGHTS RESERVED.
  11.  
  12.     Revision History:
  13.     -----------------
  14.      8/30/89 ted    Changed the clipping code returned by _clipbox funcs.
  15.  
  16.     12/07/89 jmd    preened return statements
  17.      3/28/90 jmd    ansi-fied
  18. */
  19.  
  20. #include "oakhead.h"
  21.  
  22. OSTATIC unsigned OWLPRIV opbox_clipboxpoint(opbox *clipboxp, opcoord *xp, opcoord *yp);
  23. OSTATIC unsigned OWLPRIV opwh_clipboxpoint(odim width, odim height, opcoord *xp, opcoord *yp);
  24. /* -------------------------------------------------------------------------- */
  25.  
  26. unsigned opbox_clipbox(opbox *clipboxp, opbox *boxp)
  27. /*
  28.     Return Sutherland clip code for box in display: 0 if box is completely
  29.     out of display; otherwise bits saying which side(s) it was clipped on:
  30.     1, 2, 4, 8 for right, top, left, bottom; 16 for coming through at all.
  31. */
  32. {
  33.     unsigned tlcode, brcode;
  34.  
  35.     tlcode = opbox_clipboxpoint(clipboxp, &boxp->xmin, &boxp->ymin); 
  36.     brcode = opbox_clipboxpoint(clipboxp, &boxp->xmax, &boxp->ymax); 
  37.  
  38.     if ((tlcode & brcode) != 0) {
  39.         return(0);
  40.     }
  41.     else {
  42.         return(tlcode | brcode | 16);
  43.     }
  44. }
  45. /* -------------------------------------------------------------------------- */
  46.  
  47. static unsigned OWLPRIV opbox_clipboxpoint(opbox *clipboxp, opcoord *xp, opcoord *yp)
  48. /*
  49.     Returns flag if point is out of or on edge of clipbox. This is good for
  50.     box clipping because a box with a point out and the other point on the edge
  51.     on that side should be flagged as out.
  52. */
  53. {
  54.     unsigned scode;
  55.  
  56.     scode = 0;
  57.     if (*xp <= clipboxp->xmin) { scode |= 4; *xp = clipboxp->xmin; }
  58.     if (*yp <= clipboxp->ymin) { scode |= 2; *yp = clipboxp->ymin; }
  59.     if (*xp >= clipboxp->xmax) { scode |= 1; *xp = clipboxp->xmax; }
  60.     if (*yp >= clipboxp->ymax) { scode |= 8; *yp = clipboxp->ymax; }
  61.  
  62.     return(scode);
  63. }
  64. /* -------------------------------------------------------------------------- */
  65.  
  66. unsigned opbox_clippoint(opbox *clipboxp, opcoord *xp, opcoord *yp)
  67. /*
  68.     Return Sutherland clip code for point in clipbox: 0 if point is
  69.     in; otherwise bits saying which side it's out on.
  70.     If the point is on the top or the left edge it is counted as in. This is
  71.     because drawing a pixel "at" a point means filling the grid box to the
  72.     lower right of the box in our convention.
  73. */
  74. {
  75.     unsigned scode;
  76.  
  77.     scode = 0;
  78.     if (*xp < clipboxp->xmin) { scode |= 4; *xp = clipboxp->xmin; }
  79.     if (*yp < clipboxp->ymin) { scode |= 2; *yp = clipboxp->ymin; }
  80.     if (*xp >= clipboxp->xmax) { scode |= 1; *xp = clipboxp->xmax - 1; }
  81.     if (*yp >= clipboxp->ymax) { scode |= 8; *yp = clipboxp->ymax - 1; }
  82.  
  83.     return(scode);
  84. }
  85. /* -------------------------------------------------------------------------- */
  86.  
  87. unsigned ocbox_clippos(ocbox *clipcboxp, int *rowp, int *colp)
  88. /*
  89.     Return Sutherland clip code for point in char box: 0 if point is
  90.     in box; otherwise bits saying which side it's out on.
  91. */
  92. {
  93.     unsigned scode;
  94.  
  95.     scode = 0;
  96.     if (*colp < clipcboxp->leftcol)  { scode |= 4; *colp = clipcboxp->leftcol; }
  97.     if (*rowp < clipcboxp->toprow)   { scode |= 2; *rowp = clipcboxp->toprow;  }
  98.     if (*colp > clipcboxp->rightcol) { scode |= 1; *colp = clipcboxp->rightcol;}
  99.     if (*rowp > clipcboxp->botrow)   { scode |= 8; *rowp = clipcboxp->botrow;  }
  100.  
  101.     return(scode);
  102. }
  103. /* -------------------------------------------------------------------------- */
  104.  
  105. unsigned opwh_clipbox(odim width, odim height, opbox *boxp)
  106. /*
  107.     Return Sutherland clip code for box in width/height: 0 if box is completely
  108.     out of width/height; otherwise bits saying which side(s) it was clipped on:
  109.     1, 2, 4, 8 for right, top, left, bottom; 16 for coming through at all.
  110.  
  111.     'width' and 'height' are the width and height of 0 - origin clipping box.
  112. */
  113. {
  114.     unsigned tlcode, brcode;
  115.  
  116.     tlcode = opwh_clipboxpoint(width, height, &boxp->xmin, &boxp->ymin); 
  117.     brcode = opwh_clipboxpoint(width, height, &boxp->xmax, &boxp->ymax); 
  118.  
  119.     if ((tlcode & brcode) != 0) {
  120.         return(0);
  121.     }
  122.     else {
  123.         return(tlcode | brcode | 16);
  124.     }
  125. }
  126. /* -------------------------------------------------------------------------- */
  127.  
  128. static unsigned OWLPRIV opwh_clipboxpoint(odim width, odim height, opcoord *xp, opcoord *yp)
  129. /*
  130.     Return Sutherland clip code for point in box: 0 if point is
  131.     in box; otherwise bits saying which side it's out on.
  132.  
  133.     'width' and 'height' are the width and height of 0 - origin clipping box.
  134. */
  135. {
  136.     unsigned scode;
  137.  
  138.     scode = 0;
  139.     if               (*xp <= 0)        { scode |= 4; *xp = 0; }
  140.     else if ((odim) *xp >= width)    { scode |= 1; *xp = width; }
  141.     if               (*yp <= 0)        { scode |= 2; *yp = 0; }
  142.     else if ((odim) *yp >= height)    { scode |= 8; *yp = height; }
  143.  
  144.     return(scode);
  145. }
  146. /* -------------------------------------------------------------------------- */
  147.  
  148. unsigned opwh_clippoint(odim width, odim height, opcoord *xp, opcoord *yp)
  149. /*
  150.     Return Sutherland clip code for point in box: 0 if point is
  151.     in box; otherwise bits saying which side it's out on.
  152.  
  153.     'width' and 'height' are the width and height of 0 - origin clipping box.
  154. */
  155. {
  156.     unsigned scode;
  157.  
  158.     scode = 0;
  159.     if               (*xp < 0)        { scode |= 4; *xp = 0; }
  160.     else if ((odim) *xp >= width)    { scode |= 1; *xp = width - 1; }
  161.     if               (*yp < 0)        { scode |= 2; *yp = 0; }
  162.     else if ((odim) *yp >= height)    { scode |= 8; *yp = height - 1; }
  163.  
  164.     return(scode);
  165. }
  166. /* -------------------------------------------------------------------------- */
  167.  
  168.