Java黑皮书课后题第4章:*4.2(几何:最大圆距离)最大圆面积是指球面上两个点间的距离。编写一个程序,提示用户以度为单位输入地球上两个点的经纬度,显示其最大圆距离值
*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黑皮书课后题第3章:*3.34(
- 下一篇: Java黑皮书课后题第4章:*4.3(几