Question

I'm trying to have a NSView with many different NSTextViews in it, not intersecting each other. At first, I had the idea to do the following:

for (NSString *str in frames) {

        while (CGRectIntersectsRect(NSRectToCGRect(field.frame), NSRectToCGRect(NSRectFromString(str)))) {

            CGPoint origin = [self randomPoint];

            field.frame = CGRectMake(origin.x, origin.y, sizeOfButton.width, sizeOfButton.height);


        }

    }
        NSString *string = NSStringFromRect(field.frame);

        [frames addObject:string];

Here, 'frames' is an array to which I add the coordinates of the NSTextViews, once they are cleared and supposedly not intersecting. 'field' is an NSTextField, and 'randomPoint' is a method that randomly finds an NSPoint, so that the placement of the NSTextField is random.

What happens is that I iterate through an array of rectangles, and check against each one of them that the new rectangle doesn't intersect. When he has passed this test, he himself goes into the array and a new text field is brought in.

The problem with this approach is that the NSTextField is checked against one rectangle at a time. Indeed, it might not intersect one rectangle, but intersect the following one, and unfortunately, once its coordinates are modified, it might pass the test, while having intersected the first rectangle.

Now, to simplify, does anyone know of a method, or is it possible to change my code, to check at the same time if an object intersects many other objects? Is it possible to check for an intersection between more than 2 CGRects?

Was it helpful?

Solution

As it is cleverly demonstrated here: http://static.mrfeinberg.com/bv_ch03.pdf, it seems that it is foolish and remarkably slow to try to check for multiple rectangle intersections in the manner I was using.

A much more complex and elaborate answer is illustrated in that PDF, which is completely overkill for me, but it might be the only solution in the long run.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top