简体   繁体   English

计算两点之间的距离

[英]Calculating the distance between two points

I need to create a class which calculates the distance between two points. 我需要创建一个计算两点之间距离的类。 I am stuck and I am a total beginner. 我被困了,我是一个初学者。 Here are my classes: 这是我的课程:

package org.totalbeginner.tutorial;

public class Point {

    public double x;
    public double y;

    Point(double xcoord, double ycoord){
        this.x = xcoord;
        this.y = ycoord;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }    
}

The second class. 第二节课。

package org.totalbeginner.tutorial;

public class Line {

    double x;
    double y;

    Point p1 = new Point(2.0,2.0);
    Point p2 = new Point(4.0,4.0);
    Point mp = new Point(x,y);

    public void midpoint() {
        x = (p1.getX() + p2.getX()) / 2;
        y = (p1.getY() + p2.getY()) / 2;
    }
}

I am not sure how to get a point object (the middle point) between both defined points. 我不确定如何获得两个定义点之间的点对象(中间点)。

I can create point objects but I am not sure how to return a point object through my midpoint() method that lies between those two point objects. 我可以创建点对象,但我不确定如何通过位于这两个点对象之间的midpoint()方法返回一个点对象。

The distance between two points (x1,y1) and (x2,y2) on a flat surface is: 平面上两点(x1,y1)和(x2,y2)之间的距离为:

    ____________________
   /       2          2
 \/ (y2-y1)  + (x2-x1)

But, if all you want is the midpoint of your two points, you should change your midpoint function to: 但是,如果您想要的只是两点的中点,则应将中点函数更改为:

public Point midpoint (Point p1, Point p2) {
    return new Point ((p1.getX() + p2.getX()) / 2, (p1.getY() + p2.getY()) / 2);
}

This will return a brand new point object with the points set to the middle of the given two points (without having to concern yourself with any other math). 这将返回一个全新的点对象,其中的点设置在给定的两个点的中间(不必关心任何其他数学)。 And, since your second class is a line, you only need the two end points to describe it, so I'd make some minor changes. 并且,由于你的第二课是一行,你只需要两个端点来描述它,所以我做了一些小改动。

First Point.java : First Point.java

class Point {
    double x, y;
    Point (double xcoord, double ycoord) {
        this.x = xcoord;
        this.y = ycoord;
    }
    public double getX() { return x; }
    public double getY() { return y; }
}

Then Line.java : 然后Line.java

public class Line {
    Point p1, p2;
    Line (Point point1, Point point2) {
        this.p1 = point1;
        this.p2 = point2;
    }
    public Point midpoint() {
        return new Point ((p1.getX()+p2.getX())/2, (p1.getY()+p2.getY())/2);
    }
    public double abstand() {
        return Math.sqrt(
            (p1.getX() - p2.getX()) *  (p1.getX() - p2.getX()) + 
            (p1.getY() - p2.getY()) *  (p1.getY() - p2.getY())
        );
    }
    static public void main (String args[]) {
        Line s = new Line (new Point(2.0, 2.0), new Point(5.0, 6.0));
        Point mp = s.midpoint();
        System.out.println ("Midpoint = (" + mp.getX() + "," + mp.getY() + ")");
        double as = s.abstand();
        System.out.println ("Length   = " + as);
    }
}

These two files, when compiled and run with the endpoints 2,2 and 5,6 (the hypotenuse of a classic 3/4/5 right-angled triangle), generate the correct: 这两个文件在编译并运行端点2,25,6 (经典的3/4/5直角三角形的斜边)时,生成正确的:

Midpoint = (3.5,4.0)
Length   = 5.0

简单的毕达格...根(dx ^ 2 + dy ^ 2)

Math.sqrt(Math.pow((p2.getX() - p1.getX()), 2) + Math.pow((p2.getY() - p1.getY()), 2))
 X
 +
 |\
 | \
a|  \c
 |   \
 |    \
 +-----+ 
    b   Y

Imagine X and Y are your points on a flat surface. 想象一下X和Y是你在平坦表面上的点。 Then a is Xy - Yy and b is Yx - Xx . 然后aXy - YybYx - Xx The length of c is their distance, and is the length of the hypotenuse of that triangle. c的长度是它们的距离,是该三角形的斜边的长度。 It is calculated using 它是使用计算的

sqrt(a^2 + b^2);

Since you see we are squaring a and b , the sign of them isn't relevant - it will come down to the same. 既然你看到我们正在对ab进行平方,那么它们的符号就不相关了 - 它将会归结为相同。 So this method always works, where ever the points lie. 所以这种方法总是有效的,无论哪个点都存在。

Lookup the Pythagorean theorem 查看Pythagorean theorem

Do you really need the distance, or are you trying to just get the midpoint? 你真的需要距离,还是想要获得中点? Because from your code snippet, it kind of looks like you just want to create a new point that is half-way between two existing points. 因为从您的代码片段来看,您只想创建一个位于两个现有点之间的新点。

If you're really just after the midpoint, you don't really need an entire 2nd class (ie, 'Line') to accomplish that. 如果你真的只是在中点之后,你真的不需要整个第二类(即'Line')来完成它。 Since the thing you are trying to find is also a point, it makes sense to add a constructor to your existing Point class, like so .. 既然你想要找到的东西也是一个点,那么在现有的Point类中添加一个构造函数是有意义的,就像这样。

Point(Point a, Point b)
{
  x = (a.x + b.x) / 2;
  y = (a.y + b.y) / 2;
}

.. then, elsewhere let's say you already have a couple of points you want to use this on, you use the constructor thus: ..然后,在其他地方让我们说你已经有几个想要使用它的点,你使用构造函数:

Point p1 = new Point(2,2);
Point p2 = new Point(4,4);
Point midpoint = new Point(p1, p2);

and if you really want distance between two points, that's not really an attribute of either point, so it makes sense to use a static method for that, like so 如果你真的想要两点之间的距离,这不是任何一个点的属性,所以使用静态方法是有道理的,就像这样

public static double distance(Point a, Point b)
{
  double dx = a.x - b.x;
  double dy = a.y - b.y;
  return Math.sqrt(dx * dx + dy * dy);
}

and back in the calling code, you can use it this way: 并回到调用代码中,您可以这样使用它:

Point p1 = new Point(2,2);
Point p2 = new Point(4,4);
System.out.println("Distance between them is " + Point.distance(p1, p2));

You can use a Maths function for this: 您可以使用数学函数:

public Point midpoint() {
    //Calculate the difference between the old and new x/y
    double dx = p1.getX() - p2.getX();
    double dy = p1.getY() - p2.getY();

    double newX = Math.pow(dx, 2D);
    double newY = Math.pow(dz, 2D);
    return new Point(newX, newZ);
}

Math.pow handles the issues with negative values and etc. For you, using Math.pow gives you a safe method because it has a lot of checks built inside. Math.pow使用负值等处理问题。对于您来说,使用Math.pow为您提供了一种安全的方法,因为它内置了大量的检查。

You can use the Pythagorean Theorem , as other said. 正如其他人所说, 你可以使用毕达哥拉斯定理 Here is a visually demostration from the Wolfram Demostration Project . 以下是Wolfram Demostration Project的视觉演示。

alt text http://demonstrations.wolfram.com/DistanceBetweenTwoPoints/HTMLImages/index.en/popup_5.jpg alt text http://demonstrations.wolfram.com/DistanceBetweenTwoPoints/HTMLImages/index.en/popup_5.jpg

In your second class, it looks like you're trying to set the values of x and y that are used to construct your mp variable. 在你的第二个类中,看起来你正试图设置用于构造你的mp变量的xy的值。 All your formulas are correct, but you need to consider the order that everything is executed. 你的所有公式都是正确的,但你需要考虑一切都被执行的顺序 In the code as it is, it's creating the x and y variables, which start out as 0, then the various Point s. 在代码中,它创建了xy变量,从0开始,然后是各种Point x and y are still 0, so mp is set to Point(0, 0) . xy仍为0,因此mp设置为Point(0, 0)

What you probably want to do is change the return type of midpoint to Point , so that when you call that function, you get back a Point . 你可能想要做的是将midpoint返回类型更改为Point ,这样当你调用该函数时,你会得到一个Point Then you can create a new Point object with the values you calculate. 然后,您可以使用计算的值创建新的Point对象。 It should look more like this: 它应该看起来更像这样:

public Point midpoint() {
    // calculate the middle x and y
    double x = (p1.getX() + p2.getX()) / 2;
    double y = (p1.getY() + p2.getY()) / 2;
    // now make a new Point with those values, and return it
    return new Point(x, y);
}

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

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