java - 有没有办法将 Oval 对象与 intersects() 函数一起使用?
问题描述
首先,我知道已经回答了非常相似的问题,但我还没有看到这个问题特别被问到。
我有一个椭圆和一个矩形,我想检测它们是否相交。我知道你可以用两个矩形来做到这一点:
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.
}
提前致谢!
解决方案
我在搜索时没有看到 Oval 类,因此我将使用 Ellipse 作为示例。如果您查看此处的文档,您将看到该Shape
课程的文档。
此类由两者扩展Rectangle
并且Ellipse
还包含一个intersect(Shape, Shape)
方法,因此您将能够在这两个对象上使用相交。
如果您的Oval
类和类都从继承的场景中Rectangle
扩展同一个Shape
类,它也将在您的场景中工作。intersect(Shape, Shape)
推荐阅读
- mysql - MySQL:使用 COUNT() 对总金额进行排名
- python - 仅将多索引熊猫数据框中特定日期的正值归零
- function - Tcl 8.4:扩展变量用作过程的参数
- javascript - 当我按下“全部”复选框时“我没有得到任何数据。我在这里做错了什么?
- javascript - Firefox“触摸模拟”模式为双击发出三个点击事件
- chisel - 解复用器:未连接的电线/firrtl错误
- html - 使用 contenteditable = "false" 设置 div
- python - 根据 Python pandas 中的列数据比较两个数据框
- wordpress - Wordpress 反向代理
- plsql - 重命名列,如果它有单引号