distance method

double distance(
  1. Envelope env
)

Computes the distance between this and another Envelope. The distance between overlapping Envelopes is 0. Otherwise, the distance is the Euclidean distance between the closest points.

Implementation

double distance(Envelope env) {
  if (intersectsEnvelope(env)) return 0;

  double dx = 0.0;
  if (_maxx < env._minx) {
    dx = env._minx - _maxx;
  } else if (_minx > env._maxx) dx = _minx - env._maxx;

  double dy = 0.0;
  if (_maxy < env._miny) {
    dy = env._miny - _maxy;
  } else if (_miny > env._maxy) dy = _miny - env._maxy;

  // if either is zero, the envelopes overlap either vertically or horizontally
  if (dx == 0.0) return dy;
  if (dy == 0.0) return dx;
  return math.sqrt(dx * dx + dy * dy);
}