The case of 3 rectangles

Give 3 rectanges in a 2D space. (A, B and C)

Rect-A and B are stationary, but Rect C can be moved by the user. What is the best way (when Rect-C is “dropped”) to determine which of the other 2 it is “closest” to… Note it might be possible for Rect-C to OVERLAP BOTH “A” and “B”.

In reality there are 26 stationary rectangles, and I just need to find the one the is closest (and it also must partially overlap)…

Is this what you want?

http://documentation.xojo.com/index.php/Realbasic.Rect.Contains

Not exactly… I need it a bit more refined… determining if they overlap is easy… it is trying to decide which overlaps “the most”

Ah. I see. Yes that will be a bit more difficult then I think you will have to do some comparisons.

Can you use Realbasic.Rect.Intersection to get the overlapping areas and just calculate the area of each, picking the largest area to know which is correct to use?

Dave, interesting problem to tackle. Just curious if this Is this possibly for a game?

Are all of the rectangles the same size? If so, are the “target” rectangles (A and B) located at fixed intervals, and if so, is there always a gap between them? Based on these answers, the logic below could possibly be simplified.

I’ll call Rectangle C the “Source” and Rectangles A and B the “Targets”

Off the top of my head, there are 6 possible “states” to calculate:

  1. Distance/Overlap of Upper Left corner of “Source” and Lower Right Corner of “Target”
  2. Distance/Overlap of Upper Right corner of “Source” and Lower Left Corner of “Target”
  3. Distance/Overlap of Lower Left corner of “Source” and Upper Right Corner of “Target”
  4. Distance/Overlap of Lower Right corner of “Source” and Upper Left Corner of “Target”
  5. “Source” totally overlaps “Target” (or is equal in size and location)
  6. “Source” is contained within “Target”

You could localize this logic into a general purpose method, or a method/computed property in a class. Invoke as needed for each “Source” and “Target” combo, and evaluate best nearness/overlap after each call. At the end of the run, you should have your result.

Hopefully this helps, and I’ll be interested to see what other folks can contribute.

Or, to extend Bill’s proposal: Did you have a look at Xojo.Core.Rect? It has a comparison operator, comparing by size and origin.

Are all ‘stationary rectangles’ the same size and don’t overlap among each other?

If so, I think it may work to just calculate the euclidian distance between the center of your ‘floating rectange’ and the center of each ‘stationary rectange’. Smallest distance overlaps most. (When calcualting the distance, don’t bother using the slow Sqrt() function as all you want is the smallest)

I think we may need some more info (maybe a screenshot, I’m a ‘visual’ person :slight_smile: )

No they are not all the same size. There are two constants. Either a rectangle will be of a a specific Width -or- a specific Height (ie. All rectangles will share ONE (or both) of those values)…

So distance to center won’t work (thought of that already), so I’m thinking it has to be based on the location of the top left corner of the moving rectangle, and/or the top right corner and how those two coordinates relate to the stationary rectangles… technincall the bottom corners should be considered, but those situations will be less likely due to the physics of the program

Didn’t you say there would definitely be some partial overlap on some of the rectangles? If so, you could loop through them to find out which ones intersect. Then for those, get the area of intersection to see which intersects the most… Or perhaps another Realbasic.Rect function might help. If you looked at Union, perhaps you could loop through and find the Union that makes the smallest rectangle.