   Closest rectangle (algorithm)
  What I doing is an application to run maximized windows (read application) "splitscreen" on windows (intended for widescreen monitors), and it turned out really well so I decided to add some flexibility in that you can now define your own rectangles to snap to.This is either done by dragging the window into a rectangle, maximizing a window in a rectangle, both can be described as a single point.**** BETTER EXPLANATION CAN BE FOUND IN MY OTHER POST BELOW ****The "snap rectangles" can be overlapping as it provides a benefit, and they are not required to cover the entire screen so that holes are allowed.However, I cannot seem to get a fair algorithm working, my current algorithm was made just to make it work, and what it does is to calculate distance from the center of the snaprect to the snaprect boundary, and then the distance from the center of the snaprect to the point (window or mouse) it then does "pointdst/snapdst" to make snapping to smaller snaprects easier (and to allow enclosed snaprects). (EDIT: it chooses the one with the shortest distance) But since this completely fudged with certain layouts I decided to add the constraint that it will only snap if the point is inside the snaprect.But it is necessary to always snap to one rectangle to not produce flickering and to ease snapping. However, using pythagoras for calculating the distance feels kinda akward since it produces circular snapregions which defies the fact that windows are "always" rectangular.I'm completely puzzled now as I initially though this would be a walk in the park, but getting it "feel right" wasn't as easy I expected ;)Does anyone have any idea on how to "solve" it? (I realize that my description might be a little vague but perhaps anyone knows of something similar?)**** DOWNLOAD: http://home.syranide.com/download/snap.zip ****This download is an unfinished application and as such does not represent the final quality so there are some twitches and no existing features. However, it shows my intention, maximize a window after having it started and it should only occpy either side of the screen, drag the maximized window to the other side and it should occupy that.Thanks in advance[Edited by - Syranide on June 16, 2006 7:16:50 AM]
  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?   ;  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.   ;  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 ;))   ;  Beta download of the application can be downloaded above!   ;  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.;  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.   