Is there a circle class in Java like the Rectangle class
Solution 1
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.
Solution 2
java.awt.Shape too. and all the class which implements it: Arc2D, Arc2D.Double, Arc2D.Float, Area, BasicTextUI.BasicCaret, CubicCurve2D, CubicCurve2D.Double, CubicCurve2D.Float, DefaultCaret, Ellipse2D, Ellipse2D.Double, Ellipse2D.Float, GeneralPath, Line2D, Line2D.Double, Line2D.Float, Path2D, Path2D.Double, Path2D.Float, Polygon, QuadCurve2D, QuadCurve2D.Double, QuadCurve2D.Float, Rectangle, Rectangle2D, Rectangle2D.Double, Rectangle2D.Float, RectangularShape, RoundRectangle2D, RoundRectangle2D.Double, RoundRectangle2D.Float
Solution 3
There is an ellipse2D, this is in the same way that a square is a rectangle a circle is an ellipse.
http://docs.oracle.com/javase/7/docs/api/java/awt/geom/Ellipse2D.html
user1871085
Updated on July 30, 2022Comments
-
user1871085 almost 2 years
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?
-
MadProgrammer over 11 yearsRedirect your link to the Java 7 API add I'll up vote you (Java 1.4 is nearly 10 years old :P)
-
FThompson over 11 years+1, but the more appropriate method for point intersection is
contains(double x, double y)
.