Java 圆内和圆上的点

Java 圆内和圆上的点,java,math,geometry,Java,Math,Geometry,我已经解决了这个问题,但我不确定它是否正确 用户应给出一个点的坐标,我应检查该点是否在圆内、圆外或圆上。我用距离公式来解决这个问题。 关于圆的给定信息包括: 圆以(0,0)为中心 半径为10 public static void main(String[] strings) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a point with two coordinates")

我已经解决了这个问题,但我不确定它是否正确

用户应给出一个点的坐标,我应检查该点是否在圆内、圆外或圆上。我用距离公式来解决这个问题。 关于圆的给定信息包括:

圆以(0,0)为中心 半径为10

 public static void main(String[] strings) {


    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter a point with two coordinates");
    double y1 = scanner.nextDouble();
    double x1 = scanner.nextDouble();

    //  circle is centered at 0,0
    double y2 = 0;
    double x2 = 0;

    double radius = 10;

    double distance;
    // using the distance formula to calculate the distance
    // from the point given from the user and point where circle is centered

    /**
     * distance formula
     *  d = √ ( x2 - x1 )^2 + (y2 - y1 )^2
     */

    distance = Math.pow( x2 - x1,2) + Math.pow(y2-y1,2);

    // find square root
    distance = Math.sqrt(distance);

    String result="";

    if(distance < radius) {
        result = "("+y1+" , "+x1+")" +" is within the circle";
    }
    else if(distance > radius) {
        result = y1+" , "+x1 +" is outside the circle";
    }
    else if(distance == radius) {
        result =y1+" , "+x1 +" is on the circle";
    }

    System.out.println(result);

}
publicstaticvoidmain(String[]strings){
扫描仪=新的扫描仪(System.in);
System.out.println(“输入具有两个坐标的点”);
double y1=scanner.nextDouble();
double x1=scanner.nextDouble();
//圆以0,0为中心
双y2=0;
双x2=0;
双半径=10;
双倍距离;
//使用距离公式计算距离
//从用户给定的点和圆的中心点开始
/**
*距离公式
*d=√ (x2-x1)^2+(y2-y1)^2
*/
距离=数学功率(x2-x1,2)+数学功率(y2-y1,2);
//求平方根
距离=数学sqrt(距离);
字符串结果=”;
if(距离<半径){
结果=“(“+y1+”,“+x1+”)“+”在圆圈内”;
}
否则如果(距离>半径){
结果=y1+”,“+x1+”在圆圈外”;
}
否则如果(距离==半径){
结果=y1+”,“+x1+”位于圆圈上”;
}
系统输出打印项次(结果);
}
这很好,但很马虎

不需要计算平方根。以距离平方为单位工作

然后使用
distance
等进行比较,为了清晰起见,可能需要重命名
distance
。计算平方根的成本很高,而且不精确性可能会逐渐增加,这很难控制。这在您要测试圆边缘上的点的情况下尤为重要

还考虑编写<代码>(X2-X1)*(X2-X1),而不是使用<代码> POW第二个电源。虽然Java可能(我永远不记得这肯定是我不使用它的一个足够好的理由)优化到更长的形式,但其他语言(如C)不这样做,而且不精确性也可能潜入其中

不需要计算平方根。以距离平方为单位工作

然后使用
distance
等进行比较,为了清晰起见,可能需要重命名
distance
。计算平方根的成本很高,而且不精确性可能会逐渐增加,这很难控制。这在您要测试圆边缘上的点的情况下尤为重要


还考虑编写<代码>(X2-X1)*(X2-X1),而不是使用<代码> POW第二个电源。虽然Java可能(我永远不记得这肯定是我不使用它的一个充分理由)优化到更长的形式,但其他语言(如C)不这样做,而且不精确性也可能潜入其中。

考虑使用
Math.hypot()
计算距离,并使用一些小阈值比较双值:

static final double DELTA = 1E-5;  // not necessarily 1E-5; see comments

//...

distance = Math.hypot(x2 - x1, y2 - y1);    
if (Math.abs(distance - radius) < DELTA)
   // on the circle
else if (distance > radius)
   // outside
else
   // inside
static final double DELTA=1E-5;//不一定是1E-5;见评论
//...
距离=数学正压(x2-x1,y2-y1);
if(数学绝对值(距离-半径)半径)
//外面
其他的
//里面
使用
DELTA
的原因是,通过计算获得相等双值的机会非常小。如果它们至少有一位不同,直接比较将返回false


通过应用阈值,您检查的不是点是否正好位于圆上,而是它是否位于
radius-DELTA
radius+DELTA
之间的环内。因此,
DELTA
是一种公差限值,应用时应特别选择该值,例如。G取决于绝对或相对输入不准确。

考虑使用
Math.hypot()
计算距离,并使用一些小阈值比较双值:

static final double DELTA = 1E-5;  // not necessarily 1E-5; see comments

//...

distance = Math.hypot(x2 - x1, y2 - y1);    
if (Math.abs(distance - radius) < DELTA)
   // on the circle
else if (distance > radius)
   // outside
else
   // inside
static final double DELTA=1E-5;//不一定是1E-5;见评论
//...
距离=数学正压(x2-x1,y2-y1);
if(数学绝对值(距离-半径)半径)
//外面
其他的
//里面
使用DELTA的原因是,通过计算获得相等双值的机会非常小。如果它们至少有一位不同,直接比较将返回false


通过应用阈值,您检查的不是点是否正好位于圆上,而是它是否位于
radius-DELTA
radius+DELTA
之间的环内。因此,
DELTA
是一种公差限值,应用时应特别选择该值,例如。G取决于绝对或相对输入的不准确度。

您确定此问题需要双倍输入吗?给出的例子是整数。对于整数,你可以确定点的位置;对于实数(双倍数),你不能确定“圆上”与否,这是我认为这个问题要求你使用整数的另一个原因

int x;
int y;
int centreX;
int centreY;

int deltaX = x - centreX;
int deltaY = y - centreY;

int distanceSquared = deltaX * deltaX + deltaY * deltaY;

int radiusSquared = radius * radius;

if (distanceSquared == radiusSquared) { //distance == radius
  //on circle
} else if (distanceSquared < radiusSquared) { //distance < radius
  //in circle
} else {
  //out of circle
}
性能和准确性的诀窍是不要使用
Math.sqrt
,而只使用整数

int x;
int y;
int centreX;
int centreY;

int deltaX = x - centreX;
int deltaY = y - centreY;

int distanceSquared = deltaX * deltaX + deltaY * deltaY;

int radiusSquared = radius * radius;

if (distanceSquared == radiusSquared) { //distance == radius
  //on circle
} else if (distanceSquared < radiusSquared) { //distance < radius
  //in circle
} else {
  //out of circle
}
intx;
int-y;
int centreX;
int centreY;
int deltaX=x-centreX;
int deltaY=y-centreY;
int distance squared=deltaX*deltaX+deltaY*deltaY;
int radiusSquared=半径*半径;
如果(距离平方==半径平方){//距离==半径
//在圆上
}如果(距离平方<半径平方){//距离<半径
//循环
}否则{
//不正常
}

您确定此问题需要双精度输入吗?给出的例子是整数。对于整数,你可以确定点的位置;对于实数(双倍数),你不能确定“圆上”与否,这是我认为这个问题要求你使用整数的另一个原因

int x;
int y;
int centreX;
int centreY;

int deltaX = x - centreX;
int deltaY = y - centreY;

int distanceSquared = deltaX * deltaX + deltaY * deltaY;

int radiusSquared = radius * radius;

if (distanceSquared == radiusSquared) { //distance == radius
  //on circle
} else if (distanceSquared < radiusSquared) { //distance < radius
  //in circle
} else {
  //out of circle
}
这是我的诀窍