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

问题描述

有没有办法知道交叉矩形区域在libgdx中的两个矩形之间,像c#?

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中的重叠方法只返回两个矩形是否相交的布尔值。
我已经阅读了Intersector类,但它没有提供任何操作。

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(Rectangle)获取交集矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:55