*4.2(几何:最大圆距离)最大圆面积是指球面上两个点间的距离。编写一个程序,提示用户以度为单位输入地球上两个点的经纬度,显示其最大圆距离值
题目题目概述破题运行示例代码题目
题目概述
*4.2(几何:最大圆距离)最大圆面积是指球面上两个点间的距离。编写一个程序,提示用户以度为单位输入地球上两个点的经纬度,显示其最大圆距离值
假设(x1,y1)和(x2, y2)是两个点的地理经纬度,两个点之间的最大圆距离可以用以下公式表示:d = 半径 * arccos(sin(x1) * sin(x2) + cos(x1) * cos(x2) * cos(y1 - y2))
对上式进行处理:d = R * Math.acos(Math.sin(x1) * Math.sin(x2) + Math.cos(x1) * Math.cos(x2) * Math.cos(y1 - y2))
公式中的经纬度是相对北边、西边,使用负值表示南边和东边
地球的平均半径为6 371.01km
需要使用Math.toRadians方法将度转换为弧度数
破题
获取用户输入后,需要使用Math.toRadians方法将输入的度转换为弧度数
转换后再代入两点之间的最大圆距离公式
运行示例
Enter point 1 (latitude and longitude) in degrees: 39.55 -116.25
Enter point 2 (latitude and longitude) in degrees: 41.5 87.37
The distance between the two points is 10691.79183231593
代码
import java.util.Scanner;public class Test4_2 {public static void main(String[] args) {// 获取输入Scanner input = new Scanner(System.in);System.out.println("Enter point 1 (latitude and longitude) in degrees: ");double x1 = Math.toRadians(input.nextDouble()), y1 = Math.toRadians(input.nextDouble());System.out.println("Enter point 2 (latitude and longitude) in degrees: ");double x2 = Math.toRadians(input.nextDouble()), y2 = Math.toRadians(input.nextDouble());// 地球半径final double R = 6371.01;// 代入公式double d = R * Math.acos(Math.sin(x1) * Math.sin(x2) + Math.cos(x1) * Math.cos(x2) * Math.cos(y1 - y2));// 输出System.out.println("The distance between the two points is " + d + " km");}}
Java黑皮书课后题第4章:*4.2(几何:最大圆距离)最大圆面积是指球面上两个点间的距离。编写一个程序 提示用户以度为单位输入地球上两个点的经纬度 显示其最大圆距离值
如果觉得《Java黑皮书课后题第4章:*4.2(几何:最大圆距离)最大圆面积是指球面上两个点间的距》对你有帮助,请点赞、收藏,并留下你的观点哦!