我正在尝试获取坐标之间的距离,但由于某种原因它似乎不起作用。如果有人可以提供帮助,将不胜感激!
输出:
a 点到 b 点的距离为 0.0。a 点到 b 点的距离为 0.0。a 点到 b 点的距离为 0.0。a 点到 b 点的距离为 0.0。p1 到 p2 的距离是 4.242640687119285 p1 到 p3 的距离是 12.727922061357855
package GC01;
public class Point {
private final double x;
private final double y;
private double distance;
public Point(){
x=0.0;
y=0.0;
}
public Point(double x, double y) {
this.x=x;
this.y=y;
}
public double distanceTo(Point a, Point b) {
double dx = a.x - b.x;
double dy = a.y - b.y;
distance = Math.sqrt(dx*dx + dy*dy);
return distance;
}
public String toString(){
return "The distance from Point a to Point b is " + distance +".";
}
public static void main(String[] args){
Point p0 = new Point();
Point p1 = new Point(0.0,0.0);
Point p2 = new Point(3.0,3.0);
Point p3 = new Point(9.0,9.0);
System.out.println(p0.toString());
System.out.println(p1.toString());
System.out.println(p2.toString());
System.out.println(p3.toString());
System.out.println("The distance from p1 to p2 is "+p1.distanceTo(p1,p2));
System.out.println("The distance from p1 to p3 is "+p1.distanceTo(p1,p3));
}
}