# Closest rectangle (algorithm)

This topic is 4512 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
Perhaps it's just too late at night, but it seems just too hard to get a feel for what you are trying to do, from your explanation.

Any chance you can put together a demo of the functionality you are creating?

##### Share on other sites
To calculate the distance between two points, assuming you have the x and the y coords, just do:-

dist = sqrt(x1*x2 + y1*y2);

Check the distance from the center of each side and each corner to every other of the same point on each rect. Sounds a bit confusing I know, i'll make a demo app for you later on if this post hasn't been answered.

##### Share on other sites
Yeah I'd figured it was a little vague, I already have the application running so very well I shall post it here, then you'll understand what I am after, when I get home in a few hours ;)

Ok, to (hopefully) avoid further confusion, what I have is a set of rectangles, these rectangles represent areas on the screen to which an application window can be snapped to (thus taking up the entire size of the rectangle). And to make a window snap to these rectangles it is either moved by mouse and thus the mouse pointer decides to what it should snap, or it can be maximized and thus the center of the window decides to what it should snap.

So, basically, if you set up 2 rectangles, one left and one right, if the mouse is inside the left, the window should snap to the left (e.g. fill the left side of the screen and leave the right empty). This all sounds easy, but it starts to fudge when they overlap.

What should it do? Given a set of RECTANGLES (snap rectangles) and a POINT (cursor or window center), I want to be given the most suitable rectangle for snapping the point to (e.g, which rectangle is considered "closest").

(To clarify, the most suitable rectangle IF OVERLAPPING WAS NOT ALLOWED would in "most" cases be the rectangle under the mouse)

I hope this explains it, otherwise stay tuned and the application will be uploaded later today.

rpg_code_master:
Hmm, that sounds interesting, and if you would have the time I would be most greatful if you could explain/show that slightly more.

Hmm, so what you are saying is, for every point in each "snaprect" (four corners(4) and the center of each side(4), 8 points) check the distance from that to the cursor (e.g. mouse)? What do I do with those distances?

(This seems similar to what I am doing, but I am only using the center of each "snaprect" for calculating distance. Or where you also fumbled by much description ;))

##### Share on other sites
This sounds a bit like the problem I had to solve for a card game I wrote, in which a card was being dragged around by the user via the cursor, and then it was dropped, and the app had to determine what card stack, if any, the card should be moved to.

The best approach I came up with was to calculate the intersection of the dragged rect with all of the potential drop rects, dividing by the dragged rect area so as to get a percentage, with the highest percentage winning as the drop rect.

This obviates the need to know what the cursor's doing, since the user may have started dragging by grabbing the rect anywhere within its area, and in practice really has little to do with where the user wants the dragged rect to go.

It also means that if a drop rect is smaller in size than the drag rect, the drop rect may still be chosen even though the cursor may be outside of its boundaries, as long as it contains the highest percentage of the drag rect intersections amongst all of the potential drop rects.

HTH.

##### Share on other sites
Hmm, I thought about that solution myself too, but it fails with the ability to always snap the closest, also doesn't work for moving with the mouse.

I'll try looking a little more into this, but I don't think I can use this for my purpose.

1. 1
Rutin
45
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632998
• Total Posts
3009811
• ### Who's Online (See full list)

There are no registered users currently online

×