I have an array of open NSWindow
s. How can I check to see if any of these intersect?
Asked
Active
Viewed 75 times
1 Answers
1
I'm not sure there is a way to do it aside from an exhaustive search. You can use 2 corners (the reference corner, returned by checking the windows position, and the opposite corner, or the reference corner plus the dimensions of the window) and, for each window, checking if any of the corners of the other windows lie between the current window's corners. This exhaustive approach is O(n^2), but should still be quite fast unless youre dealing with thousands of windows.
Some pseudocode:
inside (point, window):
if point.x less than window.corner1.x and point.x greater than window.corner2.x
and point.y less than window.corner1.y and point.y greater than window.corner2.y,
true.
false.
check_for_collision (window1, window2):
if (window1.corner1 inside window2) or (window1.corner2 inside window2), true.
else if window2.corner1 inside window1 or window2.corner2 inside window1, true.
otherwise, false.
check_group_of_windows (windowlist):
loop:
if size of windowlist is 1 or less, stop looping
let window be windowlist.pop
for each otherwindow in windowlist:
if check_for_collision(window, otherwindow), true.
false.
I hope that makes some sense.

Wug
- 12,956
- 4
- 34
- 54
-
Agreed. Write yourself a method to check if they overlap by checking their frame. `[[self window] frame]` would be the thing here. – guitarflow Jun 18 '12 at 20:15
-
1Couldn't I use CGRectIntersectsRect? – Yep Jun 18 '12 at 20:34