home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter61 / l61_1.c
Encoding:
C/C++ Source or Header  |  1997-06-18  |  1.2 KB  |  39 lines

  1. // Given two line endpoints, a point on a plane, and a unit normal
  2. // for the plane, returns the point of intersection of the line
  3. // and the plane in intersectpoint.
  4.  
  5. #define DOT_PRODUCT(x,y)   (x[0]*y[0]+x[1]*y[1]+x[2]*y[2])
  6.  
  7. void LineIntersectPlane (float *linestart, float *lineend,
  8.    float *planeorigin, float *planenormal, float *intersectpoint)
  9. {
  10.    float vec1[3], projectedlinelength, startdistfromplane, scale;
  11.  
  12.    vec1[0] = linestart[0] - planeorigin[0];
  13.    vec1[1] = linestart[1] - planeorigin[1];
  14.    vec1[2] = linestart[2] - planeorigin[2];
  15.  
  16.    startdistfromplane = DOT_PRODUCT(vec1, planenormal);
  17.  
  18.    if (startdistfromplane == 0)
  19.    {
  20.         // point is in plane
  21.         intersectpoint[0] = linestart[0];
  22.         intersectpoint[1] = linestart[1];
  23.         intersectpoint[2] = linestart[1];
  24.         return;
  25.    }
  26.  
  27.    vec1[0] = linestart[0] - lineend[0];
  28.    vec1[1] = linestart[1] - lineend[1];
  29.    vec1[2] = linestart[2] - lineend[2];
  30.  
  31.    projectedlinelength = DOT_PRODUCT(vec1, planenormal);
  32.  
  33.    scale = startdistfromplane / projectedlinelength;
  34.  
  35.    intersectpoint[0] = linestart[0] - vec1[0] * scale;
  36.    intersectpoint[1] = linestart[1] - vec1[1] * scale;
  37.    intersectpoint[2] = linestart[1] - vec1[2] * scale;
  38. }
  39.