查找两个矩形的重叠区域(在 C# 中)

发布于 2024-08-07 04:17:16 字数 1090 浏览 4 评论 0原文

编辑:

我用来解决问题的简单代码,以防有人感兴趣(感谢 Fredrik):

    int windowOverlap(Rectangle rect1, Rectangle rect2)
    {
        if (rect1.IntersectsWith(rect2))
        {
            Rectangle overlap = Rectangle.Intersect(rect1, rect2);
            if (overlap.IsEmpty)
                return overlap.Width * overlap.Height;
        }

        return 0;
    }

原始问题:

我想知道一种快速而肮脏的方法来检查两个矩形是否重叠以及它们是否计算了面积重叠。出于好奇,我对以下情况感兴趣:1)两个矩形中的所有线条都是垂直或水平的,或者 2)任何两个矩形的一般情况,但我真正需要的唯一答案是情况 1。

我在想沿着以下思路:

double areaOfOverlap( Rect A, Rect B)
{
    if ( A.Intersects(B) )
    {
        // calculate area
        // return area
    }

    return 0;
}

对于 A.Intersects() 我正在考虑使用分离轴测试,但是如果矩形只有水平线和垂直线,是否有更简单(更快)的检查方法?

如果矩形只有水平线和垂直线,为了计算它们相交的面积,有没有一种快速的方法?

最后,这与问题无关,但我很感激有人对一本好书/网页提供的任何建议,我可以在其中复习计算机图形学的数学。我已经离开大学一段时间了,感觉我忘记了一切:)!还有其他人有这个问题吗?

(注意:我发现这个问题与这个不同 这看起来更复杂并且没有直接回答问题)。

Edit:

Simple code I used to solve the problem in case anyone is interested (thanks to Fredrik):

    int windowOverlap(Rectangle rect1, Rectangle rect2)
    {
        if (rect1.IntersectsWith(rect2))
        {
            Rectangle overlap = Rectangle.Intersect(rect1, rect2);
            if (overlap.IsEmpty)
                return overlap.Width * overlap.Height;
        }

        return 0;
    }

Original Question:

I'd like to know a quick and dirty way to check if two rectangles overlap and if they do calculate the area of the overlap. For curiosities sake I'm interested in the case where 1) all the lines in both rectangles are either vertical or horizontal or 2) the general case for any two rectangles, but the only answer I really need is case 1.

I'm thinking along the lines of:

double areaOfOverlap( Rect A, Rect B)
{
    if ( A.Intersects(B) )
    {
        // calculate area
        // return area
    }

    return 0;
}

For A.Intersects() I was thinking of using the separating axis test, but if the rectangles have only horizontal and vertical lines is there an even simpler (faster) way to check?

And for calculating the area where they intersect is there an quick way to do it if the rectangles only horizontal and vertical lines?

Finally, this is unrelated to the question but I'd appreciate any advice someone may have on a good book / webpage where I could review the math for computer graphics. I've been out of college for a while and feel like I'm forgetting everything :)! Anyone else have that problem?

( NOTE: I found this question different than this which seems more complicated and doesn't directly answer the question. )

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

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

发布评论

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

评论(2

残疾 2024-08-14 04:17:16

也许我误解了你的问题,但 Rectangle.Intersect 方法可以完成这项工作吗?它返回相交面积,然后您可以轻松计算它的面积。

Maybe I misinterpret your question, but doesn't the Rectangle.Intersect method do the job? It returns the intersecting area, and then you can easily calculate the area of it.

轻许诺言 2024-08-14 04:17:16

听起来像是基本的碰撞检测。你看过维基百科上的这个页面吗?

Mike

编辑:Fredrik同时做出回应我做了这个,他的回答得到了我的支持(:

Sounds like basic Collision Detection. Have you looked at this page on Wikipedia?

Mike

edit: Fredrik make his response at the same time I made this one, his answer got my upvote ( :

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