編集:OKが指摘するように、このソリューションはすべての長方形が直立していることを前提としています。OPが要求するように回転した長方形で機能させるには、各長方形の角から他の長方形の最も近い辺までの距離も計算する必要があります。ただし、点が線分の両方の端点の上または下にあり、両方の線分の左または右にある場合は、ほとんどの場合、その計算を避けることができます (電話の位置 1、3、7、または 9 に対して線分)。
Agnius の答えは DistanceBetweenLineSegments() 関数に依存しています。以下は、そうでない場合の分析です。
(1) Check if the rects intersect. If so, the distance between them is 0.
(2) If not, think of r2 as the center of a telephone key pad, #5.
(3) r1 may be fully in one of the extreme quadrants (#1, #3, #7, or #9). If so
the distance is the distance from one rect corner to another (e.g., if r1 is
in quadrant #1, the distance is the distance from the lower-right corner of
r1 to the upper-left corner of r2).
(4) Otherwise r1 is to the left, right, above, or below r2 and the distance is
the distance between the relevant sides (e.g., if r1 is above, the distance
is the distance between r1's low y and r2's high y).