code: function intersectRect(rect, point) {
    var x = rect.x;
    var y = rect.y;
  
    // Rectangle intersection algorithm from:
    // http://math.stackexchange.com/questions/108113/find-edge-between-two-boxes
    var dx = point.x - x;
    var dy = point.y - y;
    var w = rect.width / 2;
    var h = rect.height / 2;
  
    if (!dx && !dy) {
      throw new Error("Not possible to find intersection inside of the rectangle");
    }
  
    var sx, sy;
    if (Math.abs(dy) * w > Math.abs(dx) * h) {
      // Intersection is top or bottom of rect.
      if (dy < 0) {
        h = -h;
      }
      sx = h * dx / dy;
      sy = h;
    } else {
      // Intersection is left or right of rect.
      if (dx < 0) {
        w = -w;
      }
      sx = w;
      sy = w * dy / dx;
    }
  
    return { x: x + sx, y: y + sy };
  }
docs: /*
  Finds where a line starting at point ({x, y}) would intersect a rectangle
  ({x, y, width, height}) if it were pointing at the rectangle's center.