function minLinearDist(A, B) { return Math.min( // vertical distances (4) between all edges (A.y) - (B.y), (A.y + A.height) - (B.y + B.height), (A.y) - (B.y + B.height), (A.y + A.height) - (B.y), // horizontal distances (4) between all edges (A.x) - (B.x), (A.x + A.width) - (B.x + B.width), (A.x) - (B.x + B.width), (A.x + A.width) - (B.x), ); }