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?
The Circle class creates a new circle with the specified radius and center location measured in pixels. Example usage. The following code creates a circle with radius of 50 pixels centered at (100,100). import javafx.
Class Rectangle. A Rectangle specifies an area in a coordinate space that is enclosed by the Rectangle object's upper-left point (x,y) in the coordinate space, its width, and its height. A Rectangle object's width and height are public fields.
The Shape interface provides definitions for objects that represent some form of geometric shape. The Shape is described by a PathIterator object, which can express the outline of the Shape as well as a rule for determining how the outline divides the 2D plane into interior and exterior points.
In Java, to draw a rectangle (outlines) onto the current graphics context, we can use the following methods provided by the Graphics/Graphics2D class: drawRect(int x, int y, int width, int height) draw3DRect(int x, int y, int width, int height, boolean raised) draw(Rectangle2D)
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With