Distance between 2 rectangles.
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Livecode Opensource Backer
- Posts: 10099
- Joined: Fri Feb 19, 2010 10:17 am
Distance between 2 rectangles.
Disclaimer: this is not really my topic: just a move from the Use-List to here for pictorial support.
-
- Livecode Opensource Backer
- Posts: 10099
- Joined: Fri Feb 19, 2010 10:17 am
Re: Distance between 2 rectangles.
My 'pain-in-the-bum' picture.
How do we define the distance between these 2 rectangles?
Re: Distance between 2 rectangles.
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?
In either case, can the rectangles overlap? What would this do to the definition?
Purpose?
-
- Livecode Opensource Backer
- Posts: 10099
- Joined: Fri Feb 19, 2010 10:17 am
Re: Distance between 2 rectangles.
If the rectangles were to overlap, surely the distance between them would be a negative value.
Re: Distance between 2 rectangles.
....implying that your points of interest are corners rather than locs. Are they permitted to overlap in your application though?
Re: Distance between 2 rectangles.
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?

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.
To build on Davidv''s question, which line do you want to measure?

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.

-
- Livecode Opensource Backer
- Posts: 10099
- Joined: Fri Feb 19, 2010 10:17 am
Re: Distance between 2 rectangles.
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?
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?
Re: Distance between 2 rectangles.
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
So the distance between two points, say, "100,100" and "200,200" would be 141.
Craig
-
- Livecode Opensource Backer
- Posts: 10099
- Joined: Fri Feb 19, 2010 10:17 am
Re: Distance between 2 rectangles.
Fine, and pretty obvious.the distance between two points
But the problem (if it is a problem at all) is the distance between 2 rectangles.
-
Re: Distance between 2 rectangles.
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}
d = sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}
Re: Distance between 2 rectangles.
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).

That you Davidv ? Whoever FINN is, that is a longer hand version of it.
Diagonal=√w2+h2 --(guess I couldn't draw power of symbols heh).

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

-
- VIP Livecode Opensource Backer
- Posts: 153
- Joined: Wed Aug 26, 2009 7:42 pm
- Contact:
Re: Distance between 2 rectangles.
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?
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.
Researchware, Inc.
-
- Livecode Opensource Backer
- Posts: 10099
- Joined: Fri Feb 19, 2010 10:17 am
Re: Distance between 2 rectangles.
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.
-
- VIP Livecode Opensource Backer
- Posts: 10048
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Distance between 2 rectangles.
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.
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Distance between 2 rectangles.
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:
With two graphics of about 150 points each, this took 10 ticks/
Craig
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
Craig