快速矩形到矩形的交点

测试2个矩形是否相交的快速方法是什么?


在Internet上进行了搜索,找到了这种单行代码(WOOT!),但我不知道如何用Javascript编写它,它似乎是用C ++的古老形式编写的。


struct

{

    LONG    left;

    LONG    top;

    LONG    right;

    LONG    bottom;

} RECT; 


bool IntersectRect(const RECT * r1, const RECT * r2)

{

    return ! ( r2->left > r1->right

        || r2->right < r1->left

        || r2->top > r1->bottom

        || r2->bottom < r1->top

        );

}


明月笑刀无情
浏览 477回答 3
3回答

饮歌长啸

.NET Framework就是这样实现Rectangle.Intersectpublic bool IntersectsWith(Rectangle rect){&nbsp; if (rect.X < this.X + this.Width && this.X < rect.X + rect.Width && rect.Y < this.Y + this.Height)&nbsp; &nbsp; return this.Y < rect.Y + rect.Height;&nbsp; else&nbsp; &nbsp; return false;}或静态版本:public static Rectangle Intersect(Rectangle a, Rectangle b){&nbsp; int x = Math.Max(a.X, b.X);&nbsp; int num1 = Math.Min(a.X + a.Width, b.X + b.Width);&nbsp; int y = Math.Max(a.Y, b.Y);&nbsp; int num2 = Math.Min(a.Y + a.Height, b.Y + b.Height);&nbsp; if (num1 >= x && num2 >= y)&nbsp; &nbsp; return new Rectangle(x, y, num1 - x, num2 - y);&nbsp; else&nbsp; &nbsp; return Rectangle.Empty;}
打开App,查看更多内容
随时随地看视频慕课网APP