home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gems / graphics / circlere.c < prev    next >
C/C++ Source or Header  |  1992-04-09  |  2KB  |  46 lines

  1. /* 
  2. Fast Circle-Rectangle Intersection Checking
  3. by Clifford A. Shaffer
  4. from "Graphics Gems", Academic Press, 1990
  5. */
  6.  
  7. #include "GraphicsGems.h"
  8.  
  9. boolean Check_Intersect(R, C, Rad)
  10.  
  11. /* Return TRUE iff rectangle R intersects circle with centerpoint C and
  12.    radius Rad. */
  13.  Box2 *R;
  14.  Point2 *C;
  15.  double Rad;
  16. {
  17.  double Rad2;
  18.  
  19.  Rad2 = Rad * Rad;
  20.  /* Translate coordinates, placing C at the origin. */
  21.  R->max.x -= C->x;  R->max.y -= C->y;
  22.  R->min.x -= C->x;  R->min.y -= C->y;
  23.  
  24.  if (R->max.x < 0)             /* R to left of circle center */
  25.        if (R->max.y < 0)         /* R in lower left corner */
  26.              return ((R->max.x * R->max.x + R->max.y * R->max.y) < Rad2);
  27.        else if (R->min.y > 0)     /* R in upper left corner */
  28.              return ((R->max.x * R->max.x + R->min.y * R->min.y) < Rad2);
  29.        else                     /* R due West of circle */
  30.              return(ABS(R->max.x) < Rad);
  31.      else if (R->min.x > 0)      /* R to right of circle center */
  32.            if (R->max.y < 0)     /* R in lower right corner */
  33.                  return ((R->min.x * R->min.x) < Rad2);
  34.        else if (R->min.y > 0)      /* R in upper right corner */
  35.              return ((R->min.x * R->min.x + R->min.y + R->min.y) < Rad2);
  36.        else                 /* R due East of circle */
  37.              return (R->min.x < Rad);
  38.      else                /* R on circle vertical centerline */
  39.            if (R->max.y < 0)     /* R due South of circle */
  40.              return (ABS(R->max.y) < Rad);
  41.        else if (R->min.y > 0)      /* R due North of circle */
  42.              return (R->min.y < Rad);
  43.        else                 /* R contains circle centerpoint */
  44.              return(TRUE);
  45. }     
  46.