title: Ray and square/rectangle intersection in 3D
body: <p>Hei. Are making a game and are looking for a ray intersection onto a square or a rectangle only in 3D space. Have search the web and found many solutions but nothing i can understand have a line and line segment intersection script in 2D but i cant figure out have to make it 3D.
  It is not important from what side it intersect the square or rectangle but it must be able to retrive the point of intersection vector so that later can be tested for distance to se if it occurred before or after other intersections on the same ray intersection.</p>
  
  <p>Any examples in python or other similar scripting languages will be greatly appreciated</p>
  
  <p><strong>Edit</strong>: Dont know have to modify the 2D to show an exaple but made a new and posting both.</p>
  
  <pre><code>//this is the exaple it test a ray onto a plane then look to se if that point is in the rectangle and saves it to test for distanse later
  list Faces; //triangle faces
  list Points; //
  
  vector FindPoint(){
      //calcute the point of intersection onto the plane and returns it
      //if it can intersect
      //else return ZERO_VECTOR
  }
  
  integer point-in-quadrilateral(){
      //return 1 if the point is in the rectangular on the plane
      //else return 0
  }
  
  default{
  
      state_entry(){
          integer n = (Faces != []); //return number of elements
          integer x = 0;
          while(x &lt; n){
              vector intersection = FindPoint( FromList(Faces, x) ); //take     out a element and runs it trough the function
              if(intersection != ZERO_VECTOR){
                  integer test = point-in-quadrilateral( FromList(Faces,     x) ); //find out if the point is in rectangular
                  if(test == 1){ //if so
                      Points += intersection; //save the point
                  }
              }
              ++x;
          }
  
          float first; //the distanse to the box intersection
          integer l = (Points != []);
          integer d;
          while(d &lt; l){
              if(Dist( FromList(Points, d) ) &lt; first) //if the new distanse     is less then first
                  return 0; //then end script
              ++d;
          }
      }
  
  }
  
  
  //this is the 2D version
  vector lineIntersection(vector one, vector two, vector three, vector four){
  float bx = two.x - one.x;
  float by = two.y - one.y;
  float dx = four.x - three.x;
  float dy = four.y - three.y; 
  float b_dot_d_perp = bx*dy - by*dx;
  if(b_dot_d_perp == 0.0) {
      return ZERO_VECTOR;
  }
  float cx = three.x-one.x; 
  float cy = three.y-one.y;
  float t = (cx*dy - cy*dx) / b_dot_d_perp; 
  if(LineSeg){ //if true tests for line segment
      if((t &lt; 0.0) || (t &gt; 1.0)){
          return ZERO_VECTOR;
      }
      float u = (cx * by - cy * bx) / b_dot_d_perp;
      if((u &lt; 0.0) || (u &gt; 1.0)) {
          return ZERO_VECTOR;
      }
  }
  
  return &lt;one.x+t*bx, one.y+t*by, 0.0&gt;; 
  </code></pre>
  
  <p>}</p>