class Capybara::Queries::SelectorQuery::Rectangle

def distance_segment_segment(l1p1, l1p2, l2p1, l2p2)

def distance_segment_segment(l1p1, l1p2, l2p1, l2p2)
  # See http://geomalgorithms.com/a07-_distance.html
  # rubocop:disable Naming/VariableName
  u = l1p2 - l1p1
  v = l2p2 - l2p1
  w = l1p1 - l2p1
  a = u.dot u
  b = u.dot v
  c = v.dot v
  d = u.dot w
  e = v.dot w
  cap_d = (a * c) - (b**2)
  sD = tD = cap_d
  # compute the line parameters of the two closest points
  if cap_d < Float::EPSILON # the lines are almost parallel
    sN = 0.0 # force using point P0 on segment S1
    sD = 1.0 # to prevent possible division by 0.0 later
    tN = e
    tD = c
  else # get the closest points on the infinite lines
    sN = (b * e) - (c * d)
    tN = (a * e) - (b * d)
    if sN.negative? # sc < 0 => the s=0 edge is visible
      sN = 0
      tN = e
      tD = c
    elsif sN > sD # sc > 1 => the s=1 edge is visible
      sN = sD
      tN = e + b
      tD = c
    end
  end
  if tN.negative? # tc < 0 => the t=0 edge is visible
    tN = 0
    # recompute sc for this edge
    if (-d).negative?
      sN = 0.0
    elsif -d > a
      sN = sD
    else
      sN = -d
      sD = a
    end
  elsif tN > tD # tc > 1 => the t=1 edge is visible
    tN = tD
    # recompute sc for this edge
    if (-d + b).negative?
      sN = 0.0
    elsif (-d + b) > a
      sN = sD
    else
      sN = (-d + b)
      sD = a
    end
  end
  # finally do the division to get sc and tc
  sc = sN.abs < Float::EPSILON ? 0.0 : sN / sD
  tc = tN.abs < Float::EPSILON ? 0.0 : tN / tD
  # difference of the two closest points
  dP = w + (u * sc) - (v * tc)
  Math.sqrt(dP.dot(dP))
  # rubocop:enable Naming/VariableName
end