以下是用于在3D空间中查找直线和点之间距离的Java代码:
import java.util.Scanner;
public class DistanceCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入直线上两点的坐标(x1, y1, z1, x2, y2, z2):");
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
double z1 = scanner.nextDouble();
double x2 = scanner.nextDouble();
double y2 = scanner.nextDouble();
double z2 = scanner.nextDouble();
System.out.println("请输入点的坐标(x, y, z):");
double x = scanner.nextDouble();
double y = scanner.nextDouble();
double z = scanner.nextDouble();
double distance = calculateDistance(x1, y1, z1, x2, y2, z2, x, y, z);
System.out.println("点到直线的距离为:" + distance);
}
public static double calculateDistance(double x1, double y1, double z1, double x2, double y2, double z2, double x, double y, double z) {
double A = (y2 - y1) * (z - z1) - (z2 - z1) * (y - y1);
double B = (z2 - z1) * (x - x1) - (x2 - x1) * (z - z1);
double C = (x2 - x1) * (y - y1) - (y2 - y1) * (x - x1);
double distance = Math.abs(A * x + B * y + C * z + (A * x1 + B * y1 + C * z1)) / Math.sqrt(A * A + B * B + C * C);
return distance;
}
}
这段代码用于计算在3D空间中,给定一条直线和一个点,求点到直线的距离。用户需要输入直线上两点的坐标和点的坐标,然后程序会计算并输出点到直线的距离。
这段代码没有直接提及腾讯云相关产品,因为在这个问题中没有与云计算相关的需求。如果您有其他与云计算相关的问题,我将很乐意为您提供帮助。
领取专属 10元无门槛券
手把手带您无忧上云