Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a circle class in Java like the Rectangle class

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?

like image 886
user1871085 Avatar asked Dec 02 '12 22:12

user1871085


People also ask

Is circle a class in Java?

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.

Is there a rectangle class in Java?

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.

What is shape in Java?

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.

How do you create a rectangle object in Java?

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)


2 Answers

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.

like image 193
In silico Avatar answered Sep 21 '22 01:09

In silico


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

like image 41
ThePerson Avatar answered Sep 22 '22 01:09

ThePerson