我想计算Java中两点之间的距离 [英] I want to calculate the distance between two points in Java

查看:782
本文介绍了我想计算Java中两点之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我写了大部分程序,可以让我确定两个圆是否重叠。

OK, so I've written most of a program that will allow me to determine if two circles overlap.

除了一个问题,我的程序没有任何问题:程序不会接受我为两个中心点之间的距离编写的代码。我可以弄清楚if / else逻辑告诉用户取决于稍后的距离值会发生什么,但我想知道现在有什么问题。 Eclipse,我正在编写的程序,告诉我应该将距离解析为数组,但我已经告诉过你它是一个int。

I have no problems whatsoever with my program aside from one issue: the program won't accept the code I've written for the distance between the two center points. I can figure out the if/else logic to tell the user what happens depending on the value of distance later, but I want to know what's wrong now. Eclipse, the program I'm coding on, is telling me that distance should be resolved to an array, but I've already told you that it's an int.

这是我的代码:

package circles;
import java.util.Scanner;

public class MathCircles {

    // variable for the distance between the circles' centers
    public static int distance; 

    // variable for the lengths of the radii combined
    public static int radii;

     public static void main(String[] args) {
    // Get the x-value of the center of circle one
    System.out.println("What is the x-coordinate for the center of circle one?");
    Scanner keyboard = new Scanner(System.in);
    int x1 = keyboard.nextInt();

    //Get the y-value of the center of circle one
    System.out.println("What is the y-coordinate for the center of circle one?");
    Scanner keyboard1 = new Scanner(System.in);
    int y1 = keyboard1.nextInt(); 

    //Get the radius length of circle one.
    System.out.println("How long is circle one's radius?");
    Scanner keyboard2 = new Scanner(System.in);
    int r1 = keyboard2.nextInt();

 // Get the x-value of the center of circle two.
    System.out.println("What is the x-coordinate for the center of circle two?");
    Scanner keyboard3 = new Scanner(System.in);
    int x2 = keyboard3.nextInt();

  //Get the y-value of the center of circle two.
    System.out.println("What is the y-coordinate for the center of circle two?");
    Scanner keyboard4 = new Scanner(System.in);
    int y2 = keyboard4.nextInt(); 

   //Get the radius length of circle two.
    System.out.println("How long is circle two's radius?");
    Scanner keyboard5 = new Scanner(System.in);
    int r2 = keyboard5.nextInt();

/*
 * OK, so now I have the location of the two circles' centers,
    * as well as the lengths of their radii. 
    * The circles are intersecting IF THE DISTANCE BETWEEN THE TWO CENTERS
    * IS EQUAL TO OR LESS THAN THE COMBINED LENGTHS OF THE RADII.
    * Now I need to get some math done. 
    */

//calculate the combined lengths of the radii

radii = r1 + r2; 

//calculate the distance
distance = Math.sqrt((x1-x2)(x1-x2) + (y1-y2)(y1-y2));




}

}


推荐答案

根据@ trashgod的评论,这是计算距离的最简单方法:

Based on the @trashgod's comment, this is the simpliest way to calculate distance:

double distance = Math.hypot(x1-x2, y1-y2);

来自文档 Math.hypot


返回: sqrt(x²+y²)没有中间溢出或下溢。

Returns: sqrt(x²+ y²) without intermediate overflow or underflow.

这篇关于我想计算Java中两点之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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