java - 我想计算Java中两点之间的距离

标签 java math distance

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

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

这是我的代码:

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 的评论,这是计算距离的最简单方法:

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

来自 documentation Math.hypot:

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

关于java - 我想计算Java中两点之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14431032/

相关文章:

从 (x,y) 位置计算对角线元素

javascript - JavaScript 中的距离数学并在循环中实现它?

geolocation - 地理空间坐标和距离(以公里为单位)

java - Spring Reactive - 收集一系列分页结果作为所有结果的 Mono

java - 如何将列表的元素添加到另一个列表?

java - 3d vector 的全局角度

python - 对 Pandas 列中的元素应用函数,分组到另一列

java - 用于嵌入的 Headless JDK7

java.lang.VerifyError : Expecting a stackmap frame at branch target 73

java - 毕达哥拉斯定理的代码优化