Code archives/Algorithms/Distance between 2 squares (2d)

This code has been declared by its author to be Public Domain code.

Download source code

Distance between 2 squares (2d) by Matthew Smith2010
Thanks to Jesse and andy_mc for assistance.

Topic: http://blitzbasic.com/Community/posts.php?topic=90903
function dist(x1#,y1#,x2#,y2#)
distance# = sqr((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
return distance
end function 

OR

d = sqr((x2-x1)^2+(y2-y1)^2)

Comments

Kryzon2010
This isn't actually the distance between two squares, but rather two points in a 2D space.

The distance between two squares should be computed (if they are not both axis-aligned) by finding which vertex is closer to the other square (so you'll end up with two most closer vertices, one for each square), and getting the distance between these close vertices.


Code Archives Forum