计算两点之间的距离 [英] Calculating the distance between two points

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

问题描述

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

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

第二类。

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.

推荐答案

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

The distance between two points (x1,y1) and (x2,y2) on a flat surface is:

    ____________________
   /       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.

第一个 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; }
}

然后 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);
    }
}

这两个文件在编译并与端点一起运行时 2,2 5,6 (经典3/4/5直角三角形的斜边),生成正确的:

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:

Midpoint = (3.5,4.0)
Length   = 5.0

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

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