在Java中是否有类似Rectangle类的圆类 [英] Is there a circle class in Java like the Rectangle class

查看:448
本文介绍了在Java中是否有类似Rectangle类的圆类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在编写一个快速程序,遇到需要使用圆圈进行碰撞检测的地方。但据我所知,只有Rectangle类具有.intersects(Point p)方法。有没有像我可以用同样方式使用的圆圈?

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?

推荐答案

有一个名为 Ellipse2D c $ c> java.awt.geom 您可以使用的包,因为它有一些看起来像您正在寻找的方法。宽度等于其高度的椭圆是一个圆圈。

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) 

测试指定的坐标是否在
Shape的边界内,如内部性定义所述。

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)

测试 Shape 与指定矩形区域的内部相交。

Tests if the interior of the Shape intersects the interior of a specified rectangular area.

请注意 Ellipse2D 是一个抽象类;你会使用它的一个嵌套子类 Ellipse2D.Double Ellipse2D.Float ,唯一的区别是使用的数据类型存储尺寸。

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类的圆类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆