有没有办法将 Oval 对象与 intersects() 函数一起使用?

Is there a way to use an Oval object with the intersects() function?

首先,我知道已经回答了非常相似的问题,但我还没有看到这个问题特别被问到。

我有一个椭圆形和一个矩形,我想检测它们是否相交。我知道你可以像这样用两个矩形来做到这一点:

if (new Rectangle(x1, y1, w1, h1).intersects(new Rectangle(x2, y2, w2, h2))) {

    //code here for when collision occurs.

}

是否可以用这样的椭圆来做?

if (new Oval(x1, y1, w1, h1).intersects(new Rectangle(x2, y2, w2, h2))) {

    //code here for when collision occurs.

}

提前致谢!

我在搜索时没有看到椭圆 class,所以我将使用椭圆作为示例。如果您查看文档 here,您将看到 Shape class 的文档。

此 class 由 RectangleEllipse 扩展,还包含一个 intersect(Shape, Shape) 方法,因此您将能够在这两个上使用 intersect对象。

如果您的 Oval class 和 Rectangle class 在您的场景中都扩展了相同的 Shape Class intersect(Shape, Shape)继承自,它也适用于您的场景。