Advertisement
Guest User

intersect.cpp

a guest
May 29th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. /*
  2.  * ______________________ _______    ________ ___  ___
  3.  * \__    ___/\_   _____/ \      \  /  _____/|   | |  \
  4.  *   |    |    |    __)_  /   |   \/   \  ___|   | |  |
  5.  *   |    |    |        \/    |    \    \_\  \   |_|  |
  6.  *   |____|   /_______  /\____|__  /\______  /________/
  7.  * Game Engine.       \/         \/        \/          
  8.  *
  9.  * (c) 8BitPimp (2013)
  10.  */
  11. #include "intersect.h"
  12.  
  13.  
  14. //
  15. // if [x1,y1] lies inside the box then this will fail
  16. //
  17. //
  18. bool intersect_line_box
  19. (
  20.     sRayBox *i
  21. )
  22. {
  23.     // find x1 x2 dimension style box
  24.     float bx1 = i->bx - i->bw * 0.5f;
  25.     float bx2 = i->bx + i->bw * 0.5f;
  26.     float by1 = i->by - i->bh * 0.5f;
  27.     float by2 = i->by + i->bh * 0.5f;
  28.     //
  29.     if ( i->x2 < i->x1 ) { swapf( &bx1, &bx2 ); }
  30.     if ( i->y2 < i->y1 ) { swapf( &by1, &by2 ); }
  31.     //
  32.     float dx = (i->x2 - i->x1);
  33.     float dy = (i->y2 - i->y1);
  34.     //
  35.     float t1x = (bx1 - i->x1) / dx;
  36.     float t2x = (bx2 - i->x1) / dx;
  37.     float t1y = (by1 - i->y1) / dy;
  38.     float t2y = (by2 - i->y1) / dy;
  39.     //
  40.     if ( maxf( t1x, t1y ) < minf( t2x, t2y ) )
  41.     {
  42.         //
  43.         float d = maxf( t1x, t1y );
  44.         //
  45.         if ( d > 1.0f ) return false;
  46.         if ( d < 0.0f ) return false;
  47.         //
  48.         i->ix = i->x1 + d * (i->x2 - i->x1);
  49.         i->iy = i->y1 + d * (i->y2 - i->y1);
  50.         //
  51.         return true;
  52.     }
  53.     //
  54.     return false;
  55. }
  56.  
  57. //
  58. bool intersect_ray_box
  59. (
  60.     sRayBox *i
  61. )
  62. {
  63.     // find x1 x2 dimension style box
  64.     float bx1 = i->bx - i->bw * 0.5f;
  65.     float bx2 = i->bx + i->bw * 0.5f;
  66.     float by1 = i->by - i->bh * 0.5f;
  67.     float by2 = i->by + i->bh * 0.5f;
  68.     // swap to find the closest planes
  69.     if ( i->x2 < i->x1 ) { swapf( &bx1, &bx2 ); }
  70.     if ( i->y2 < i->y1 ) { swapf( &by1, &by2 ); }
  71.     // differences
  72.     float dx = (i->x2 - i->x1);
  73.     float dy = (i->y2 - i->y1);
  74.     //
  75.     float t1x = (bx1 - i->x1) / dx;
  76.     float t2x = (bx2 - i->x1) / dx;
  77.     float t1y = (by1 - i->y1) / dy;
  78.     float t2y = (by2 - i->y1) / dy;
  79.     //
  80.     if ( maxf( t1x, t1y ) < minf( t2x, t2y ) )
  81.     {
  82.         //
  83.         float d = maxf( t1x, t1y );
  84.         //
  85.         i->ix = i->x1 + d * (i->x2 - i->x1);
  86.         i->iy = i->y1 + d * (i->y2 - i->y1);
  87.         //
  88.         return true;
  89.     }
  90.     //
  91.     return false;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement