gpt4 book ai didi

java - Libgdx - 从 Rectangle.overlap(Rectangle) 获取相交矩形

转载 作者:行者123 更新时间:2023-11-29 09:38:00 29 4
gpt4 key购买 nike

有没有办法知道 libgdx 中两个 Rectangle 之间的交集 Rectangle 面积,就像 C# 中的 Rectangle http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.rectangle.intersect.aspx

我需要获取两个矩形之间的交集矩形区域,但是 libgdx 中的重叠方法只返回 boolean 值,无论两个矩形是否相交。我已经阅读了 Intersector 类,但它没有提供任何相关内容。

最佳答案

事实上,LibGDX 并没有内置这个功能,所以我会这样做:

/** 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;
}

关于java - Libgdx - 从 Rectangle.overlap(Rectangle) 获取相交矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17267221/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com