计算两点之间的距离

mrt*_*181 20 java

我需要创建一个计算两点之间距离的类.我被困了,我是一个初学者.这是我的课程:

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;
    }    
}
Run Code Online (Sandbox Code Playgroud)

第二节课.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我不确定如何获得两个定义点之间的点对象(中间点).

我可以创建点对象,但我不确定如何通过midpoint()位于这两个点对象之间的方法返回点对象.

pax*_*blo 49

平面上两点(x1,y1)和(x2,y2)之间的距离为:

    ____________________
   /       2          2
 \/ (y2-y1)  + (x2-x1)
Run Code Online (Sandbox Code Playgroud)

但是,如果您想要的只是两点的中点,则应将中点函数更改为:

public Point midpoint (Point p1, Point p2) {
    return new Point ((p1.getX() + p2.getX()) / 2, (p1.getY() + p2.getY()) / 2);
}
Run Code Online (Sandbox Code Playgroud)

这将返回一个全新的点对象,其中的点设置在给定的两个点的中间(不必关心任何其他数学).并且,由于你的第二课是一行,你只需要两个端点来描述它,所以我做了一些小改动.

第一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; }
}
Run Code Online (Sandbox Code Playgroud)

然后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);
    }
}
Run Code Online (Sandbox Code Playgroud)

编译并使用端点2,25,6(经典的3/4/5直角三角形的斜边)编译时,这两个文件生成正确的:

Midpoint = (3.5,4.0)
Length   = 5.0
Run Code Online (Sandbox Code Playgroud)

  • 有罪,你的荣誉:-) (3认同)
  • 我相信我的"平面"条款@duffymo会涵盖这一点. (3认同)

Oli*_*Oli 20

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

Math.sqrt(Math.pow((p2.getX() - p1.getX()), 2) + Math.pow((p2.getY() - p1.getY()), 2))
Run Code Online (Sandbox Code Playgroud)


Joh*_*itb 11

 X
 +
 |\
 | \
a|  \c
 |   \
 |    \
 +-----+ 
    b   Y
Run Code Online (Sandbox Code Playgroud)

想象一下X和Y是你在平坦表面上的点.然后aX.y - Y.ybY.x - X.x.长度c是它们的距离,是该三角形的斜边的长度.它是使用计算的

sqrt(a^2 + b^2);
Run Code Online (Sandbox Code Playgroud)

因为你看我们是平方ab,他们的标志是不相关的-它会回落到相同.所以这种方法总是有效的,无论哪个点都存在.

查找 Pythagorean theorem

  • 或者您可以使用Math.hypot() - 稍短 (3认同)

Jus*_*eff 6

你真的需要距离,还是想要获得中点?因为从您的代码片段来看,您只想创建一个位于两个现有点之间的新点.

如果你真的只是在中点之后,你真的不需要整个第二类(即'Line')来完成它.既然你想要找到的东西也是一个点,那么在你现有的Point类中添加一个构造函数是有意义的,就像这样.

Point(Point a, Point b)
{
  x = (a.x + b.x) / 2;
  y = (a.y + b.y) / 2;
}
Run Code Online (Sandbox Code Playgroud)

..然后,在其他地方让我们说你已经有几个想要使用它的点,你使用构造函数:

Point p1 = new Point(2,2);
Point p2 = new Point(4,4);
Point midpoint = new Point(p1, p2);
Run Code Online (Sandbox Code Playgroud)

如果你真的想要两点之间的距离,这不是任何一个点的属性,所以使用静态方法是有道理的,就像这样

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);
}
Run Code Online (Sandbox Code Playgroud)

并回到调用代码中,您可以这样使用它:

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