Distance between 2 rectangles.

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Distance between 2 rectangles.

Post by richmond62 » Tue Feb 16, 2021 8:55 am

Disclaimer: this is not really my topic: just a move from the Use-List to here for pictorial support.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Re: Distance between 2 rectangles.

Post by richmond62 » Tue Feb 16, 2021 8:56 am

Screenshot 2021-02-16 at 9.53.44.png
Screenshot 2021-02-16 at 9.53.44.png (5.28 KiB) Viewed 235320 times
-
My 'pain-in-the-bum' picture.

How do we define the distance between these 2 rectangles?

Davidv
Posts: 77
Joined: Sun Apr 09, 2006 1:51 am

Re: Distance between 2 rectangles.

Post by Davidv » Tue Feb 16, 2021 9:43 am

Is this a definitional question or a request for an effective generalised algorithm (and coding)?

In either case, can the rectangles overlap? What would this do to the definition?

Purpose?

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Re: Distance between 2 rectangles.

Post by richmond62 » Tue Feb 16, 2021 10:58 am

If the rectangles were to overlap, surely the distance between them would be a negative value.

Davidv
Posts: 77
Joined: Sun Apr 09, 2006 1:51 am

Re: Distance between 2 rectangles.

Post by Davidv » Tue Feb 16, 2021 11:24 am

....implying that your points of interest are corners rather than locs. Are they permitted to overlap in your application though?

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Distance between 2 rectangles.

Post by bogs » Tue Feb 16, 2021 12:15 pm

You definitely would need to know what your measuring too, or you could setup a series of answers and display what you choose (or all measurements at once).

To build on Davidv''s question, which line do you want to measure?

Image

The easiest way I can think of would be a series of tests, using points to draw an actual line, then reporting the length of that line.

For instance, the logic might go something like -

put the loc of graphic 1 into lineStart; put the loc of graphic 2 into lineEnd
new line(lineStart, lineEnd)
--- put the length of the line either in pixels or the format your looking for using math to convert the pixels...

For any other parts of the rectangles, you would use top(left, right) or bottom(left, right) of each rect to get the line start and end.

To find out if one is 'negatively' spaced or not, you'd use the items of the above, subtracting the start from the end item of each pair.
Image

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Re: Distance between 2 rectangles.

Post by richmond62 » Tue Feb 16, 2021 3:00 pm

What becomes obvious is that this is a bit Wittgensteinian in so far
as there is no answer because the wrong question is being asked.

Here are a few questions that might get answers:

1. What is the distance between the 2 closest points of 2 rectangles?

2. What is the distance between the centres of 2 rectangles?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Distance between 2 rectangles.

Post by dunbarx » Tue Feb 16, 2021 3:08 pm

We are on a cartesian plane. The distances can be found at once by simply taking any two points and doing the math. If the locs, or topLefts whatever, are known then use the pythagorean theorem.

So the distance between two points, say, "100,100" and "200,200" would be 141.

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Re: Distance between 2 rectangles.

Post by richmond62 » Tue Feb 16, 2021 3:44 pm

the distance between two points
Fine, and pretty obvious.

But the problem (if it is a problem at all) is the distance between 2 rectangles.
-
maths3b.gif

FiNN-6001
Posts: 8
Joined: Mon Feb 15, 2021 11:46 pm

Re: Distance between 2 rectangles.

Post by FiNN-6001 » Tue Feb 16, 2021 4:04 pm

Distance is (mathematically speaking) the shortest distance, therefore you would choose the bottom-left corner of the top-right rectangle and the top-right corner of the bottom-left rectangle. (Note, I used to have a diffrent account but I forgot my login detalins so I made this new account and I cant see some of the images because of the post count so excuse me if I got my rectangles wrong). and then just apply it to the forumla

d = sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Distance between 2 rectangles.

Post by bogs » Tue Feb 16, 2021 4:06 pm

I told you earlier, use a line, or any form that is rectangular really (line just looks better if you see it), then apply the following to the rectangle using the two locs of the objects as the top or bottom opposites -

Diagonal=√w2+h2 --(guess I couldn't draw power of symbols heh).
Image

That you Davidv ? Whoever FINN is, that is a longer hand version of it.
Image

paul@researchware.com
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Wed Aug 26, 2009 7:42 pm
Contact:

Re: Distance between 2 rectangles.

Post by paul@researchware.com » Tue Feb 16, 2021 5:59 pm

So I was the original poster on the LC USE-LIST (Email list). The original poll was what the most common mental definition people had to the question of "the distance between to arbitrary rectangles (r,1,r2)" Do people think of that as:

center to center?
nearest corner to nearest corner?
nearest edge to nearest edge?
something else?

I was curious (and it is also relevant to a LC coding project I am working on) what *most* people think of when thinking of the how to define the distance between 2 rectangles.

For any given definition of the distance, I have enough of a geometry background to calculate the distance, so again, real question is what is the most common thing most people think of as the definition of the distance between 2 rectangles?
Paul Dupuis
Researchware, Inc.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Re: Distance between 2 rectangles.

Post by richmond62 » Tue Feb 16, 2021 6:09 pm

Screenshot 2021-02-16 at 19.06.11.png
Screenshot 2021-02-16 at 19.06.11.png (12.09 KiB) Viewed 235134 times
-
I will try to answer my understanding of this by presenting 2 amorphous blobs, rather like
two islands in the sea.

Surely the distance between these 2 shapes (or any other shapes) is the distance between the parts of their perimeters
which are nearest.

So, nothing, as such, to do with vertices or corners.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10048
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Distance between 2 rectangles.

Post by FourthWorld » Tue Feb 16, 2021 6:12 pm

The most common answer seems to be that distance describes gap between nearest edges.

Center is as much a reflection of size, muddying a measure of gap, so nearest edges would seem most appropriate.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Distance between 2 rectangles.

Post by dunbarx » Tue Feb 16, 2021 7:00 pm

Regarding Richmond's blobs, it should not take LC too much time to run through all the points, even if there might be a few hundred in each blob.

So if you have two weird graphics, put this in a button script somewhere:

Code: Select all

on mouseUp
   put the points of grc 1 into g1
   put the points of grc 2 into g2
   
   repeat with  y = 1 to the number of lines of g1
      repeat with x = 1 to the number of lines of g2
         put trunc(findDistance(line y of g1,line x of g2)) & return after accum 
      end repeat
   end repeat
   sort accum numeric
   answer line 1 of accum
end mouseUp

function findDistance a,b
   return sqrt(abs(item 1 of a - item 1 of b) ^2 + abs(item 2 of a - item 2 of b) ^2)
end findDistance
With two graphics of about 150 points each, this took 10 ticks/

Craig

Post Reply