我是编程新手,正在寻求帮助来查找点之间的距离
我目前拥有的是:
import java.util.*;
class WS8Q2 {
public static void main(String[] args){
Scanner in = new Scanner (System.in);
double x0=0;
double x1=0;
double y0=0;
double y1=0;
System.out.println("Please enter 4 numbers");
x0=in.nextDouble();
double distance = (x0, y1, x1, y0);
System.out.print(answer);
}
public static double distance (x0, y1, x1,y0) {
return Math.Sqrt ((x1 - y0) * (y1-y0) + (x1-x0) * (x1-x0));
}
我真的不确定如何做到这一点,因为我以前从未做过距离,而且我完全迷失了,尝试查找某些公式,但我发现唯一对我有用的是在这个网站 link这对我来说似乎没什么意义
编辑:这是问题以及我想要实现的目标:
Design a function distance that takes four fractional number parameters x0, y0, x1, and y1, and returns the distance between points (x0, y0) and (x1, y1). (Look up the formula if you can’t remember it!)
最佳答案
两点(x0, y0)
和(x1, y1)
的距离方程应为:
Math.Sqrt ((x1 - x0) * (x1-x0) + (y1-y0) * (y1-Y0));
考虑采用 OOP:声明一个具有 (x ,Y) 属性的类 Point,声明一个 setLocation(a, b)
函数并声明 getDistance(Point)
函数:
class Point
{
double x;
double y;
Point(double x, double y)
{
this.x = x;
this.y = y;
}
public double getDistance(Point p)
{
// now implement it
}
}
此外,如果您还不知道 java 有一个 Point2D具有 distance(double px, double py)
的类功能。
关于java - 点 (x0, y0) 和 (x1, y1) 之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20552676/