检查 CGRect 是否与矩形数组相交

发布于 2024-12-11 19:06:18 字数 896 浏览 0 评论 0原文

我试图拥有一个 NSView,其中包含许多不同的 NSTextView,而不是彼此相交。起初,我有执行以下操作的想法:

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];

这里,“frames”是一个数组,一旦它们被清除并且假定不相交,我将 NSTextView 的坐标添加到其中。 'field'是一个NSTextField,'randomPoint'是随机找到一个NSPoint的方法,这样NSTextField的放置是随机的。

发生的情况是,我迭代一组矩形,并检查每个矩形是否与新矩形不相交。当他通过这个测试时,他自己进入数组并引入一个新的文本字段。

这种方法的问题是 NSTextField 一次针对一个矩形进行检查。事实上,它可能不会与一个矩形相交,而是与下一个矩形相交,不幸的是,一旦修改其坐标,它可能会通过测试,同时与第一个矩形相交。

现在,为了简单起见,有谁知道一种方法,或者是否可以更改我的代码,以同时检查一个对象是否与许多其他对象相交?是否可以检查 2 个以上 CGRect 之间的交集?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

临风闻羌笛 2024-12-18 19:06:18

正如这里巧妙地演示的:http://static.mrfeinberg.com/bv_ch03.pdf,尝试以我使用的方式检查多个矩形交叉点似乎是愚蠢且非常慢的。

该 PDF 中说明了一个更加复杂和详细的答案,这对我来说完全是多余的,但从长远来看,它可能是唯一的解决方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文