How can I calculate the minimum distance between two rectangles? It is easy for rectangles which have no angles(0 degrees) but for rotated rectangles with any different angles I do not know how to do it.. Do you recommend any way?
WhiteFlare
How can I calculate the minimum distance between two rectangles? It is easy for rectangles which have no angles(0 degrees) but for rotated rectangles with any different angles I do not know how to do it.. Do you recommend any way?
WhiteFlare
0) Check either they intersect first (try to take point from one rectangle and check either it inside other rectangle). There are several ways to do it. One method (not the best one, but easy to explain) is the following. let A1,A2,A3,A4 - rectangle points, T - some other point. then count squares for trinagles S1 = (A1,A2,T), S2 = S(A2,A3,T), S3 = S(A3, A4, T), S4 = S(A4, A1, A2). let S_rectangle be reactangle square. Then T lies inside rectangle <=> S1 + S2 + S3 + S4 = S_rectangle.
If reactangles don't intersect each other, then do these steps.
1) calculate coordinates of all 8 points of 2 rectangles.
2) take minimum among all 4 * 4 = 16 pairs of points (points from different rectangles). let's denote it min_1.
3) Then, take some point from the first rectangle (4 ways to do it), take 4 segments of another rectangle (4 ways), check either perpendicular from that point to that segment gets inside segment. Take the mininmum of such perpendiculars. let's denote it min_2
4) The same as in 3, but take point from the second rectangle, lines from the first: you get min_3.
5) result = min(min_1, min_2, min_3)