public class Point {
private double X, Y;
public Point() {
setPoint(0.0,0.0);
}
public Point (double X, double Y) {
setPoint(X,Y);
}
public void setPoint(double X, double Y) {
this.X = X;
this.Y = Y;
}
public double getX() {
return this.X;
}
public double getY() {
return this.Y;
}
/**
* Compute the distance of this Point to the supplied Point x.
*
* @param x Point from which the distance should be measured.
* @return The distance between x and this instance
*/
public double distance(Point x) {
double d= Math.pow(this.X-X,2)+Math.pow(this.Y-Y,2);
return Math.sqrt(d);
}
我正在尝试计算“原始点”与提供的点x的距离。我不确定我是否做对了。我主要关心的是:
如何参考原始点和提供的点的坐标?这里的数学是基础知识,因此我对此充满信心。
任何帮助表示赞赏。 PS我是Java的新手。
所以我也在考虑在函数中为我的点赋值:
public double distance(Point x) {
Point x = new Point(X,Y);
double d= Math.pow(this.X-x.X,2)+Math.pow(this.Y-x.Y,2);
return Math.sqrt(d);
}
这样可以吗
最佳答案
您没有在参数中使用。
public double distance(Point other) {
double d = Math.pow(other.getX()- getX(), 2) + Math.pow(other.getY() - getY(), 2);
return Math.sqrt(d);
}
关于java - 如何在Java中计算原始点与提供的点之间的距离,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60262698/