获取欧氏距离的 Java 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26172891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java methods getting euclidean distance
提问by user3444564
public class Point
{
// Placeholders for xcoordinate, ycoordinate, and quadrants
int xcoord = 0;
int ycoord =0;
double distance = 0.0;
String quadrant = ("NW");
//moveUp changes the y coordinate
void moveUp (int x) {
int moveUp = ycoord + x;
ycoord= moveUp;
System.out.println(moveUp);
}
// moveDown changes the y coordinate
void moveDown (int y){
int moveDown = ycoord - y;
ycoord =moveDown;
System.out.println(moveDown);}
// moveLeft changes the x coordinate
void moveLeft (int f){
int moveLeft = xcoord -f ;
xcoord = moveLeft;
System.out.println(moveLeft);}
// moveRight changes the x coordinate
void moveRight (int h) {
int moveRight = xcoord +h ;
xcoord = moveRight;
System.out.println(moveRight);}
// This takes the y coordinate and the xcoordinate and places it into a quadrant. Then it returns the quadrant.
String quadrant () {
if (xcoord >= 0 && ycoord >=0){
return quadrant = ("NE"); }
else if ( xcoord < 0 && ycoord < 0 ) {
return quadrant = ("SW");}
else if (xcoord <0 && ycoord >0 ){
return quadrant = ("NW");}
else if (xcoord >0 && ycoord <0){
return quadrant = ("SE");}
else {
return quadrant = ("Origin");}
}
//euclidean distance (?)
Point distance (Point other) {
Point result = new Point();
result.ycoord = Math.abs (ycoord - other.ycoord);
result.xcoord = Math.abs (xcoord- other.xcoord);
result.distance = Math.sqrt((result.ycoord)*(result.ycoord) +(result.xcoord)*(result.xcoord));
System.out.println(result);
return result;
}
}
Here is the whole code. What I'm having trouble with is for some reason my distance method isn't working properly and I don't know why. It returns Point@24a42c89
, instead of any number. Please let me know why it is returning this instead of a number. Thanks so much. I'm a beginner and have little knowledge on Java.
这是整个代码。我遇到的问题是由于某种原因我的距离方法不能正常工作,我不知道为什么。它返回Point@24a42c89
,而不是任何数字。请让我知道为什么它返回这个而不是一个数字。非常感谢。我是初学者,对Java知之甚少。
采纳答案by Daniel Ribeiro Moreira
Your method returns a Point because you return result
which is an instance of Point. If you want a double (value) for the distance you should do it this way:
您的方法返回一个 Point,因为您返回的result
是 Point 的一个实例。如果你想要一个双倍(值)的距离,你应该这样做:
//euclidean distance (?)
double distance (Point other) {
Point result = new Point();
result.ycoord = Math.abs (ycoord - other.ycoord);
result.xcoord = Math.abs (xcoord- other.xcoord);
result.distance = Math.sqrt((result.ycoord)*(result.ycoord) +(result.xcoord)*(result.xcoord));
System.out.println(result);
return result.distance;
}
}
回答by Kick Buttowski
In you distance function
在你距离函数
Point distance (Point other) {
...
rest of your code
...
System.out.println(result); <-- you have tired to print out the object
return result;
}
to rectify your issue you need to print out the filed of the object that you are looking for which is distance
要纠正您的问题,您需要打印出您正在寻找的对象的字段 distance
System.out.println(result.distance);
One suggestion
一个建议
you can change return type distance
function from Point
to int
您可以将返回类型distance
函数从更改Point
为int
why?
为什么?
Because return the object Point
does not represent the value of distance
因为返回的对象Point
不代表距离的值
回答by Code-Apprentice
You are getting a Point
as a result because you are returning a Point
object. There is no reason to declare a Point
object when calculating the distance between two points. The results of all calculations here are numbers, so declare all variables as double
:
你得到一个Point
结果是因为你正在返回一个Point
对象。Point
在计算两点之间的距离时,没有理由声明一个对象。这里所有计算的结果都是数字,因此将所有变量声明为double
:
double distance (Point other) {
double deltaX = ycoord - other.ycoord;
double deltaY = xcoord - other.xcoord;
double result = Math.sqrt(deltaX*deltaX + deltaY*deltaY);
return result;
}
Note there is no need for Math.abs()
since squaring two numbers always results in a non-negative number.
请注意,没有必要,Math.abs()
因为将两个数字平方总是导致非负数。
This also removes the need of storing a distance
value in every point. This should make sense because a point is a pair of (x, y) coordinates, but does nothave an inherent distance. Rather "distance" is a relationship between two points.
这也消除了distance
在每个点存储值的需要。这应该是有意义,因为一个点是一对(X,Y)坐标,但不具有内在的距离。相反,“距离”是两点之间的关系。