0

I have an array of open NSWindows. How can I check to see if any of these intersect?

Yep
  • 653
  • 5
  • 20

1 Answers1

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