function positive_intersection_with_rect(pos, dir, rectPos, rectSize) {
  var left = rectPos[0];
  var right = rectPos[0] + rectSize[0];
  var top = rectPos[1];
  var bottom = rectPos[1] + rectSize[1];

  dir = vnorm(dir);

  // Check if pos is outside of rectangle.
  if (clamp(left, pos[0], right) != pos[0] || clamp(top, pos[1], bottom) != pos[1]) {
    return undefined;
  }

  // Check intersection with top segment.
  if (dir[1] < 0) {
    var t = (top - pos[1]) / dir[1];
    var x = pos[0] + dir[0] * t;
    if (clamp(left, x, right) == x) {
      return [x, top];
    }
  }

  // Check intersection with bottom segment.
  if (dir[1] > 0) {
    var t = (bottom - pos[1]) / dir[1];
    var x = pos[0] + dir[0] * t;
    if (clamp(left, x, right) == x) {
      return [x, bottom];
    }
  }

  // Check intersection with left segment.
  if (dir[0] < 0) {
    var t = (left - pos[0]) / dir[0];
    var y = pos[1] + dir[1] * t;
    if (clamp(top, y, bottom) == y) {
      return [left, y];
    }
  }

  // Check intersection with right segment.
  if (dir[0] > 0) {
    var t = (right - pos[0]) / dir[0];
    var y = pos[1] + dir[1] * t;
    if (clamp(top, y, bottom) == y) {
      return [right, y];
    }
  }

  // Failed to compute intersection due to numerical precision.
  return undefined;
}
/** Compute a point of intersection of a ray with a rectangle.
 ** Args:
 **   pos: array of 2 numbers, representing ray source.
 **   dir: array of 2 numbers, representing ray direction.
 **   rectPos: array of 2 numbers, representing rectangle corner position.
 **   rectSize: array of 2 positive numbers, representing size of the rectangle.
 **
 ** Returns:
 ** - undefined, if pos is outside of the rectangle.
 ** - undefined, in case of a numerical error.
 ** - array of 2 numbers on a rectangle boundary, in case of an intersection.
 */
Compute a point of intersection of a ray with a rectangle.
Args:
pos: array of 2 numbers, representing ray source.
dir: array of 2 numbers, representing ray direction.
rectPos: array of 2 numbers, representing rectangle corner position.
rectSize: array of 2 positive numbers, representing size of the rectangle.

- undefined, if pos is outside of the rectangle.
- undefined, in case of a numerical error.
- array of 2 numbers on a rectangle boundary, in case of an intersection.
Compute a point of intersection of a ray with a rectangle . Args : pos : array of 2 numbers representing ray source . dir : array of 2 numbers representing ray direction . rectPos : array of 2 numbers representing rectangle corner position . rectSize : array of 2 positive numbers representing size of the rectangle . - undefined if pos is outside of the rectangle . - undefined in case of a numerical error . - array of 2 numbers on a rectangle boundary in case of an intersection .
function positive_intersection_with_rect(pos, dir, rectPos, rectSize) {
  var left = rectPos[0];
  var right = rectPos[0] + rectSize[0];
  var top = rectPos[1];
  var bottom = rectPos[1] + rectSize[1];
  dir = vnorm(dir);
  if (clamp(left, pos[0], right) != pos[0] || clamp(top, pos[1], bottom) != pos[1]) {
    return undefined;
  }
  if (dir[1] < 0) {
    var t = (top - pos[1]) / dir[1];
    var x = pos[0] + dir[0] * t;
    if (clamp(left, x, right) == x) {
      return [x, top];
    }
  }
  if (dir[1] > 0) {
    var t = (bottom - pos[1]) / dir[1];
    var x = pos[0] + dir[0] * t;
    if (clamp(left, x, right) == x) {
      return [x, bottom];
    }
  }
  if (dir[0] < 0) {
    var t = (left - pos[0]) / dir[0];
    var y = pos[1] + dir[1] * t;
    if (clamp(top, y, bottom) == y) {
      return [left, y];
    }
  }
  if (dir[0] > 0) {
    var t = (right - pos[0]) / dir[0];
    var y = pos[1] + dir[1] * t;
    if (clamp(top, y, bottom) == y) {
      return [right, y];
    }
  }
  return undefined;
}
function positive_intersection_with_rect ( pos , dir , rectPos , rectSize ) { var left = rectPos [ 0 ] ; var right = rectPos [ 0 ] + rectSize [ 0 ] ; var top = rectPos [ 1 ] ; var bottom = rectPos [ 1 ] + rectSize [ 1 ] ; dir = vnorm ( dir ) ; if ( clamp ( left , pos [ 0 ] , right ) != pos [ 0 ] || clamp ( top , pos [ 1 ] , bottom ) != pos [ 1 ] ) { return undefined ; } if ( dir [ 1 ] < 0 ) { var t = ( top - pos [ 1 ] ) / dir [ 1 ] ; var x = pos [ 0 ] + dir [ 0 ] * t ; if ( clamp ( left , x , right ) == x ) { return [ x , top ] ; } } if ( dir [ 1 ] > 0 ) { var t = ( bottom - pos [ 1 ] ) / dir [ 1 ] ; var x = pos [ 0 ] + dir [ 0 ] * t ; if ( clamp ( left , x , right ) == x ) { return [ x , bottom ] ; } } if ( dir [ 0 ] < 0 ) { var t = ( left - pos [ 0 ] ) / dir [ 0 ] ; var y = pos [ 1 ] + dir [ 1 ] * t ; if ( clamp ( top , y , bottom ) == y ) { return [ left , y ] ; } } if ( dir [ 0 ] > 0 ) { var t = ( right - pos [ 0 ] ) / dir [ 0 ] ; var y = pos [ 1 ] + dir [ 1 ] * t ; if ( clamp ( top , y , bottom ) == y ) { return [ right , y ] ; } } return undefined ; }
Compute a point of intersection of a ray with a rectangle.
Compute a point of intersection of a ray with a rectangle .
// Check if pos is outside of rectangle. // Check intersection with top segment. // Check intersection with bottom segment. // Check intersection with left segment. // Check intersection with right segment. // Failed to compute intersection due to numerical precision.