简体   繁体   English

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

[英]I want to calculate the distance between two points in Java

OK, so I've written most of a program that will allow me to determine if two circles overlap. 好的,所以我编写了大部分程序,可以让我确定两个圆是否重叠。

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. 我可以弄清楚if / else逻辑告诉用户取决于稍后的距离值会发生什么,但我想知道现在有什么问题。 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. Eclipse,我正在编写的程序,告诉我应该将距离解析为数组,但我已经告诉过你它是一个int。

Here is my code: 这是我的代码:

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));

    }    
}

Based on the @trashgod's comment, this is the simpliest way to calculate distance: 根据@ trashgod的评论,这是计算距离的最简单方法:

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

From documentation of Math.hypot : Math.hypot 文档

Returns: sqrt(x²+ y²) without intermediate overflow or underflow. 返回: sqrt(x²+ y²)没有中间上溢或下溢。

Unlike maths-on-paper notation, most programming languages (Java included) need a * sign to do multiplication. maths-on-paper符号不同,大多数编程语言(包括Java)需要*符号来进行乘法运算。 Your distance calculation should therefore read: 因此,您的距离计算应为:

distance = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));

Or alternatively: 或者:

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

This may be OLD, but here is the best answer: 这可能是旧的,但这是最好的答案:

    float dist = (float) Math.sqrt(
            Math.pow(x1 - x2, 2) +
            Math.pow(y1 - y2, 2) );

You need to explicitly tell Java that you wish to multiply. 您需要明确告诉Java您希望成倍增加。

(x1-x2) * (x1-x2) + (y1-y2) * (y1-y2)

Unlike written equations the compiler does not know this is what you wish to do. 与编写的公式不同,编译器不知道这是你想要做的。

Based on the @trashgod's comment, this is the simpliest way to calculate >distance: 根据@ trashgod的评论,这是计算>距离的最简单方法:

double distance = Math.hypot(x1-x2, y1-y2); From documentation of Math.hypot: 从Math.hypot的文档:

Returns: sqrt(x²+ y²) without intermediate overflow or underflow. 返回: sqrt(x²+ y²)没有中间上溢或下溢。

Bob 短发

Below Bob's approved comment he said he couldn't explain what the 在鲍勃批准的评论之下,他说他无法解释什么

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

did. 没有。 To explain a triangle has three sides. 解释一个三角形有三个方面。 With two points you can find the length of those points based on the x,y of each. 有两个点,您可以根据每个点的x,y找到这些点的长度。 Xa=0, Ya=0 If thinking in Cartesian coordinates that is (0,0) and then Xb=5, Yb=9 Again, cartesian coordinates is (5,9) . Xa=0, Ya=0如果在笛卡尔坐标中考虑(0,0)然后Xb=5, Yb=9再次,笛卡尔坐标为(5,9) So if you were to plot those on a grid, the distance from from x to another x assuming they are on the same y axis is +5 . 因此,如果您要绘制网格上的那些,假设它们位于同一y轴上,从x到另一个x的距离为+5 and the distance along the Y axis from one to another assuming they are on the same x-axis is +9 . 假设它们在相同的x轴上,沿着Y轴从一个到另一个的距离是+9 (think number line) Thus one side of the triangle's length is 5, another side is 9. A hypotenuse is (想想数字线)因此三角形长度的一边是5,另一边是9.斜边是

 (x^2) + (y^2) = Hypotenuse^2

which is the length of the remaining side of a triangle. 这是三角形剩余边的长度。 Thus being quite the same as a standard distance formula where 因此与标准距离公式完全相同

 Sqrt of (x1-x2)^2 + (y1-y2)^2 = distance 

because if you do away with the sqrt on the lefthand side of the operation and instead make distance^2 then you still have to get the sqrt from the distance. 因为如果你取消操作左侧的sqrt而是使距离^ 2,那么你仍然需要从远处获得sqrt。 So the distance formula is the Pythagorean theorem but in a way that teachers can call it something different to confuse people. 因此,距离公式是毕达哥拉斯定理,但在某种程度上,教师可以称之为不同的东西来混淆人们。

Math.sqrt返回一个double,所以你也必须将它强制转换为int

distance = (int)Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));

You could also you Point2D Java API class: 你也可以使用Point2D Java API类:

public static double distance(double x1, double y1, double x2, double y2)

Example: 例:

double distance = Point2D.distance(3.0, 4.0, 5.0, 6.0);
System.out.println("The distance between the points is " + distance);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM