# [OT] Poll: What does it mean for 1 rect to be 'within' a certain distance of another rect?

Alex Tweedly alex at tweedly.net
Tue Feb 16 08:26:47 EST 2021

```Hi Henk,

No, it's maybe not obvious but it does take all cases into account.

If the smallest distance is between corners of each rectangle - only
then does the 'sqrt' have real meaning. In all other cases, one or other
of the DX and DY comes out as 0.

------
|    |
| R1 |  ------
|    |  |    |
|    |  | R2 |
------  |    |
-----

So if there is an overlap in one dimension, (Y above) then DY is
calculated as 0 - and the result is the distance in X (i.e. sqrt(DX*DX+0) ).

Alex.

On 16/02/2021 07:45, Henk van der Velden via use-livecode wrote:
> Alex,
> No one would challenge your definition I think, but your solution only takes the corner points into account, right? So this wouldn’t give correct results in case the rectangles have different sizes or are positioned obliquely.
>
> Henk
>
>
>> On 16 Feb 2021, at 03:12, use-livecode-request at lists.runrev.com wrote:
>>
>> For me, a more precise problem specification is:
>>
>> Given two rectangles (in regular Livecode format), the distance between
>> them is :
>>
>> ?- if they touch or overlap, distance is zero.
>>
>> ?- otherwise, it's the smallest distance from any point in R1 to any
>> point in R2.
>>
>> Which leads to a simple solution
>>
>>> function rdist R1, R2
>>> ?? local DX, DY
>>> ?? put max( item 1 of R1 - item 3 of R2, item 1 of R2 - item 3 of R1,
>>> 0) into DX
>>> ?? put max( item 2 of R1 - item 4 of R2, item 2 of R2 - item 4 of R1,
>>> 0) into DY
>>> ?? return sqrt( DX*DX + DY * DY)
>>> end rdist
>>>
>> Alex.
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode

```