【问题标题】:Is there a way to use an Oval object with the intersects() function?有没有办法将 Oval 对象与 intersects() 函数一起使用?
【发布时间】:2019-05-14 20:39:46
【问题描述】:

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

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

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.

}

提前致谢!

【问题讨论】:

    标签: java geometry collision oval


    【解决方案1】:

    我在搜索时没有看到 Oval 类,因此我将使用 Ellipse 作为示例。如果您查看文档here,您将看到Shape 类的文档。

    该类由RectangleEllipse 扩展,还包含intersect(Shape, Shape) 方法,因此您可以在这两个对象上使用相交。

    如果您的Oval 类和Rectangle 类都在您的场景中扩展了相同的Shape 类,而intersect(Shape, Shape) 是从其中继承而来的,那么它也可以在您的场景中使用。

    【讨论】:

      猜你喜欢
      • 2021-10-02
      • 2018-03-15
      • 2016-11-01
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多