Libgdx - 从Rectangle.overlap获取交集矩形(矩形) [英] Libgdx - Get Intersection Rectangle from Rectangle.overlap(Rectangle)

查看:359
本文介绍了Libgdx - 从Rectangle.overlap获取交集矩形(矩形)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法知道像矩形在C#中的http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.rectangle.intersect.aspx

Is there any way to know the intersection Rectangle area between two Rectangles in libgdx like the Rectangle in c# http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.rectangle.intersect.aspx ?

我需要获得两个矩形,但在libgdx重叠法之间的交集矩形区域只返回布尔值两个矩形是否相交与否。
我已阅读部门间类,但它提供了没事做。

I need to get the intersection rectangle area between that two rectangles but the overlap method in libgdx only return boolean value whether two rectangles are intersect or not. I have read Intersector class but it provides nothing to do that.

感谢您

推荐答案

事实上,LibGDX没有内置此功能,所以我会做这样的事情:

Indeed, LibGDX does not have this functionality built in, so I would do something like this:

/** Determines whether the supplied rectangles intersect and, if they do,
 *  sets the supplied {@code intersection} rectangle to the area of overlap.
 * 
 * @return whether the rectangles intersect
 */
static public boolean intersect(Rectangle rectangle1, Rectangle rectangle2, Rectangle intersection) {
    if (rectangle1.overlaps(rectangle2)) {
        intersection.x = Math.max(rectangle1.x, rectangle2.x);
        intersection.width = Math.min(rectangle1.x + rectangle1.width, rectangle2.x + rectangle2.width) - intersection.x;
        intersection.y = Math.max(rectangle1.y, rectangle2.y);
        intersection.height = Math.min(rectangle1.y + rectangle1.height, rectangle2.y + rectangle2.height) - intersection.y;
        return true;
    }
    return false;
}

这篇关于Libgdx - 从Rectangle.overlap获取交集矩形(矩形)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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