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

Alex Tweedly alex at tweedly.net
Mon Feb 15 19:53:23 EST 2021

On 15/02/2021 23:21, Sean Cole (Pi) via use-livecode wrote:
> Hi,
> No point reinventing the wheel..

But most of those links actually did not have this particular wheel, or
anything like it.

ugly, inefficient and lengthy (45 lines in Python) solution. [OK, that
45 includs blank and comments :-) !!

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.

> Lots of other way. Distance between two near or far edges. distance between
> corners, distance to centers, or area median, area of space between,
> overal usage area minus the area used by the two rectangles. Math is fun.
>
> Google (distance between two rectangles):
>
> https://stackoverflow.com/questions/4978323/how-to-calculate-distance-between-two-rectangles-context-a-game-in-lua
>
> https://math.stackexchange.com/questions/2724537/finding-the-clear-spacing-distance-between-two-rectangles
>
>
> https://www.programmersought.com/article/44554760896/
>
> etc
>
> On Mon, 15 Feb 2021 at 22:53, Paul Dupuis via use-livecode <
> use-livecode at lists.runrev.com> wrote:
>
>> This is an Off Topic informal poll of sorts, but related to LiveCode as
>> I am writing a LiveCode expression to determine if 2 arbitrary
>> rectangles (r1,r2) are with some distance d (in px) of one another. In
>> considering this problem, the questions comes up: What is meant by
>> rectangles being within a distance d of one another. What is the 'd'
>> measured from?
>>
>> center to center? Easiest is many ways, but I don't think this is what
>> most people would think of.
>>
>> adjacent edge to adjacent edge? This is harder (I think), but I think
>> this is what more people intuitively think of. To me, implicit in the
>> visual concept of 2 rects being within some distance of one another is
>> that they are NOT overlapping, but that some gap exists between the
>>
>> Something else? What does 2 rects  being 'within' d pixels of one
>> another mean to you, if not one of the two above options?
>>
>> Maybe there is a exact mathematical definition of what 2 rectangles
>> being within distance d of one another is, but, if there is, I am
>> unfamiliar with it.
>>
>>
>> _______________________________________________
>> 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
>>
> _______________________________________________
> 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