To convert degrees to radians and then calculate the
distance between two points on a sphere (like the Earth), you can use the
following steps in Java:
1. Convert Degrees to Radians
The conversion formula from degrees to radians is:
Fig 1 |
2. Calculate Distance on a Sphere (Haversine Formula)
The Haversine formula is used to calculate the distance
between two points on the surface of a sphere given their latitude and
longitude in radians.
Formula:
Fig 2 |
Where:
- ϕ1,ϕ2
are the latitudes in radians,
- λ1,λ2
are the longitudes in radians,
- Δϕ=Ï•2−Ï•1
,
- Δλ=λ2−λ1
,
- R is
the radius of the Earth (mean radius = 6,371 km).
Java Code Example:
java
public class
DegreeToRadianAndDistance { // Method to convert degrees to radians public static double
degreesToRadians(double degrees) { return Math.toRadians(degrees); } public static void degToRad(){ double pointX,pointY,x = 0,y = 0,distance
= 10,angle = 30; pointX = Math.cos(Math.toRadians( angle )); pointY = Math.sin(Math.toRadians( angle )); System.out.println("pointX -->"+pointX+"
pointY-->"+pointY); pointX = x + distance *
Math.cos(Math.toRadians( angle )); pointY = y + distance *
Math.sin(Math.toRadians( angle )); System.out.println("pointX -->"+pointX+"
pointY-->"+pointY); angle = 45; pointX = x + distance *
Math.cos(Math.toRadians( angle )); pointY = y + distance *
Math.sin(Math.toRadians( angle )); System.out.println("pointX -->"+pointX+"
pointY-->"+pointY); angle = 60; pointX = x + distance *
Math.cos(Math.toRadians( angle )); pointY = y + distance *
Math.sin(Math.toRadians( angle )); System.out.println("pointX -->"+pointX+"
pointY-->"+pointY); angle = 90; pointX = x + distance *
Math.cos(Math.toRadians( angle )); pointY = y + distance *
Math.sin(Math.toRadians( angle )); System.out.println("pointX -->"+pointX+"
pointY-->"+pointY); angle = 120; pointX = x + distance *
Math.cos(Math.toRadians( angle )); pointY = y + distance *
Math.sin(Math.toRadians( angle )); System.out.println("pointX -->"+pointX+"
pointY-->"+pointY); angle = 180; pointX = x + distance *
Math.cos(Math.toRadians( angle )); pointY = y + distance *
Math.sin(Math.toRadians( angle )); System.out.println("pointX -->"+pointX+"
pointY-->"+pointY); angle = 210; pointX = x + distance *
Math.cos(Math.toRadians( angle )); pointY = y + distance *
Math.sin(Math.toRadians( angle )); System.out.println("pointX -->"+pointX+"
pointY-->"+pointY); angle = 290; pointX = x + distance *
Math.cos(Math.toRadians( angle )); pointY = y + distance *
Math.sin(Math.toRadians( angle )); System.out.println("pointX -->"+pointX+"
pointY-->"+pointY); } // Method to calculate the distance
between two points on Earth using Haversine formula public static double
haversineDistance(double lat1, double lon1, double lat2, double lon2) { final int R = 6371; // Radius of the
Earth in kilometers // Convert latitude and longitude
from degrees to radians double lat1Rad =
degreesToRadians(lat1); double lon1Rad =
degreesToRadians(lon1); double lat2Rad =
degreesToRadians(lat2); double lon2Rad =
degreesToRadians(lon2); // Calculate the differences double dLat = lat2Rad - lat1Rad; double dLon = lon2Rad - lon1Rad; // Haversine formula double a = Math.sin(dLat / 2) *
Math.sin(dLat / 2) + Math.cos(lat1Rad) *
Math.cos(lat2Rad) * Math.sin(dLon / 2) *
Math.sin(dLon / 2); double c = 2 *
Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); // Calculate the distance double distance = R * c; return distance; // Distance in
kilometers } public static void main(String[] args) { // Example coordinates (latitude,
longitude) double lat1 = 52.5200; // Berlin double lon1 = 13.4050; double lat2 = 48.8566; // Paris double lon2 = 2.3522; // Convert degree to radians and
calculate the distance double distance =
haversineDistance(lat1, lon1, lat2, lon2); System.out.println("Distance
between Berlin and Paris: " + distance + " km"); } } |
Explanation:
- degreesToRadians(double
degrees): Converts degrees to radians using Math.toRadians(), which
internally multiplies the input by π180\frac{\pi}{180}180π.
- haversineDistance():
- Takes
the latitude and longitude of two points in degrees.
- Converts
them to radians.
- Applies
the Haversine formula to calculate the great-circle distance between the
points on the Earth's surface.
- main:
- Example
coordinates are given for Berlin and Paris.
- The
distance between these two cities is calculated and printed.
Output:
text
pointX -->0.8660254037844387
pointY-->0.49999999999999994 pointX -->8.660254037844387
pointY-->4.999999999999999 pointX -->7.0710678118654755
pointY-->7.071067811865475 pointX -->5.000000000000001
pointY-->8.660254037844386 pointX -->6.123233995736766E-16 pointY-->10.0 pointX -->-4.999999999999998
pointY-->8.660254037844387 pointX -->-10.0
pointY-->1.2246467991473533E-15 pointX -->-8.660254037844386
pointY-->-5.000000000000001 pointX -->3.4202014332566897
pointY-->-9.396926207859083 |
This code calculates the approximate distance between two
geographical points on the Earth, using their latitude and longitude values,
which are first converted from degrees to radians. The Haversine formula is
particularly useful for computing the distance between points on a sphere,
accounting for the curvature of the Earth.
Degrees To Radians |
0 Comments