Is there a circle class in Java like the Rectangle class

后端 未结 3 1364
清歌不尽
清歌不尽 2020-12-18 21:43

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 tha

相关标签:
3条回答
  • 2020-12-18 21:49

    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.

    0 讨论(0)
  • 2020-12-18 21:54

    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

    0 讨论(0)
  • 2020-12-18 21:59

    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

    0 讨论(0)
提交回复
热议问题