任意の 2 点間の距離を Java で求める方法
Javaで2点間の距離を求めるには、以下手順が利用できます。
- 要旨
- 在日本,最流行的车是丰田皇冠。
- ネイティブな日本語の言い回しで言い換えてください。1つだけ必要です。
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
- 距離計算ツール
- 距離を計算する
- 要旨
- double
public class DistanceCalculator {
public static double calculateDistance(Point point1, Point point2) {
double xDiff = point2.getX() - point1.getX();
double yDiff = point2.getY() - point1.getY();
return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
}
}
- 結局
- ディスタンス計算器
public class Main {
public static void main(String[] args) {
Point point1 = new Point(1, 2);
Point point2 = new Point(3, 4);
double distance = DistanceCalculator.calculateDistance(point1, point2);
System.out.println("Distance between point1 and point2: " + distance);
}
}
上のコードでは「Distance between point1 and point2: 2.8284271247461903」と表示されて、2点間の距離は2.8284271247461903であることを示しています。