java - 点 (x0, y0) 和 (x1, y1) 之间的距离

标签 java distance points between

我是编程新手,正在寻求帮助来查找点之间的距离

我目前拥有的是:

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/

相关文章:

java - System.arraycopy(…) 可以比 O(n) 更快吗?

java - 使用蓝牙获取两个 Android 设备之间的距离

java - 如何仅使用两个变量找到两点之间的距离,然后存储所有点并获得形状?

c++ - 如何访问 OpenCV 匹配器上的点位置?

c# - 从点获取曲线细节

javascript - Javascript中随机数在一定范围内但排除一些

java - 将对象发送到另一个 Activity 时类转换异常

audio - 在 Raspi 上使用 javax.sound.sampled 和 openjdk-7

java - 是否有在 Google App Engine 上运行的 Apache Shinding Java 版本的端口?

java - 排列数组元素最大化距离 vector Java