問題描述
嘿,我正在編寫一個快速程序,但遇到了一些需要使用圓圈進行碰撞檢測的地方.但據我所知,只有 Rectangle 類具有 .intersects(Point p) 方法.有沒有類似圓圈的東西可以用同樣的方式使用?
有一個類叫Ellipse2D
在您可以使用的 java.awt.geom
包中,因為它有一些看起來像您的方法重新尋找.寬度等于高度的橢圓是圓.
contains
的其中一個重載允許您測試圓點碰撞:
boolean contains(double x, double y)
測試指定的坐標是否在邊界內Shape
,正如內在定義所描述的那樣.
另一個名為 intersects
的函數允許您測試圓與矩形的碰撞:
boolean intersects(double x, double y, double w, double h)
測試 Shape
的內部是否與指定矩形區域的內部相交.
注意 Ellipse2D
是一個抽象類;您可以使用其中一個嵌套子類 Ellipse2D.Double
或 Ellipse2D.Float
,唯一的區別是用于存儲維度的數據類型.
Hey I was writing a quick program and something came across where I need to use a circle for collision detection. But as far as I know, there is only the Rectangle class that has the .intersects(Point p) method. Is there anything like a circle that I could use the same way?
There is a class called Ellipse2D
in the java.awt.geom
package that you can use, since it has some methods that appears to be what you're looking for. An ellipse with a width equal to its height is a circle.
One of the overloads for contains
allows you to test for circle-point collisions:
boolean contains(double x, double y)
Tests if the specified coordinates are inside the boundary of the
Shape
, as described by the definition of insideness.
Another function called intersects
allows you to test for circle-rectangle collisions:
boolean intersects(double x, double y, double w, double h)
Tests if the interior of the
Shape
intersects the interior of a specified rectangular area.
Note that Ellipse2D
is an abstract class; you would use one of its nested subclasses Ellipse2D.Double
or Ellipse2D.Float
, the only difference being the data type used to store the dimensions.
這篇關于Java中是否有像Rectangle類這樣的circle類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!