Maybe it's just because I'm tired... but I can't figure this out.
Say I have two rectangles, and I know the minimum x and y and the maximum x and y of each. How do I find the shortest distance between the two?
Maybe it's just because I'm tired... but I can't figure this out.
Say I have two rectangles, and I know the minimum x and y and the maximum x and y of each. How do I find the shortest distance between the two?
What exactly do you mean by 'the shortest distance'? What are you trying to do?
Maybe it's just because I'm tired... but I can't figure this out.
Say I have two rectangles, and I know the minimum x and y and the maximum x and y of each. How do I find the shortest distance between the two?
The shortest distance will either be a horizontal or vertical line (in which case any of a range of points can be used as the endpoints of the line), or a line connecting two of the corners.
If your x coordinates overlap, then the distance is vertical and you need only find the minimum distance between the minimum y of one rectangle and the maximum of the other. (If the y's overlap too, the distance should properly be 0.)
If your y coordinates overlap but x coords do not, the distance is horizontal and you need only find the minimum distance between the x's.
If neither overlap, find the minimum x distance and minimum y distance anyway, and use Pythagoras to get a diagonal.
rect var x1;y1;x2;y2
proc/DistanceTo(rect/R2) // assume always that x1<=x2 and y1<=y2 // that can be handled in New() var/dx=max(x1,R2.x1)-min(x2,R2.x2) var/dy=max(y1,R2.y1)-min(y2,R2.y2) if(dx<0) return max(dy,0) if(dy<0) return dx return sqrt(dx*dx+dy*dy)
So it actually works out to be even easier than it sounds.
The dx and dy vars will be negative in the event of any overlap. If both are negative, the max(dy,0) bit should ensure a return value of 0.
Of course, if these rectangles can be rotated then all this goes out the window.
What exactly do you mean by 'the shortest distance'? What are you trying to do?