Distance between two rectangles

BlitzPlus Forums/BlitzPlus Programming/Distance between two rectangles

Mr Brine(Posted 2004) [#1]
Hi All

Any one know how to calculate the distance between 2 rectangles???

If the two rectangles overlap it should return a distance of 0, else it should return the distance in pixels of the two closest points of the rectanges.

Ta

Mr Brine


semar(Posted 2004) [#2]
2D or 3D ?

If 2D, you can use ImagesCollide to check if the non transparent images of rectangle are colliding or overlapping each other.

To calculate the distance you want, I guess is a bit more complex.

What do you want to achieve ?

Sergio.


Mr Brine(Posted 2004) [#3]
hi Sergio

2d, I was gonna use rectsoverlap to determin if they are touching, I cant really use the imagescollide. I want to check area's which may or may not be = to the size of an image frame. I did come up with one idea though whilst walking the dog, which is:

1. split the area into a grid of 9 somthing like a o&x's board. having one rect being the centre square ie:

_|_|_
_|_|_
| |

2. Determin which segment of the o&x board which the other
rect lies in using the following rules:

if the other rect cant see a side of the centre square by looking either vertical or horizontal then it goes in one
of the corner squares. The two nearest corners are used to determin the distance

else the two nearest sides are used to determin the distance!

3) calculate the distance (this bit aint a problem)

Hope you understand this algorythm. (is it possible to upload images with messages??)

Il'll let you know wether this idea works or not when Ive implemented it!

Cheers

Mr Brine


Mr Brine(Posted 2004) [#4]
got it working! check it out (if yer arsed)

http://www.blitzbasic.com/codearcs/codearcs.php?code=911