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);
}
这可以吗?
在方法距离中,您将另一个点作为名称为x
的变量传递(不是很好的名字,并且可以使用该变量访问其字段和方法:
public double distance(Point x) {
double currentPointX = this.getX();
double otherPointX = x.getX();
}
Y值也一样,然后可以使用这些值进行数学运算。
您未在参数中使用。
public double distance(Point other) {
double d = Math.pow(other.getX()- getX(), 2) + Math.pow(other.getY() - getY(), 2);
return Math.sqrt(d);
}
Math.sqrt((x1-x2)(x1-x2)+(y1-y2)(y1-y2))>] >>