发现两个矩形的重叠区域(在C#) [英] Finding the overlapping area of two rectangles (in C#)

查看:2259
本文介绍了发现两个矩形的重叠区域(在C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:

简单的代码,我用来解决的情况下,任何人有兴趣(感谢弗雷德里克)问题:

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:

我想知道和一个快速和肮脏的方法来检查,如果两个矩形重叠,如果他们这样做计算重叠的区域。对于好奇心的缘故,我很感兴趣在1)所有两个矩形线条垂直或水平,或2)一般情况下,任何两个矩形的情况,但唯一的答案,我真正需要的是情况1。

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.

我的线沿线的思考:

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

    return 0;
}

有关A.Intersects()我想用分离轴测试,但如果矩形仅具有水平和垂直线有一个更简单的(更快)的方法来检查

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. )

推荐答案

也许我误解你的问题,但不将的 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.

这篇关于发现两个矩形的重叠区域(在C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆