From b0f210db18e12d4173a492552a83f8bcf10bfaad Mon Sep 17 00:00:00 2001 From: Bigeon Emmanuel Date: Wed, 14 Feb 2024 14:17:43 +0100 Subject: [PATCH] [tool] GPS computer --- .../fr/u_paris/gla/project/utils/GPS.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/main/java/fr/u_paris/gla/project/utils/GPS.java diff --git a/src/main/java/fr/u_paris/gla/project/utils/GPS.java b/src/main/java/fr/u_paris/gla/project/utils/GPS.java new file mode 100644 index 0000000..1aaca79 --- /dev/null +++ b/src/main/java/fr/u_paris/gla/project/utils/GPS.java @@ -0,0 +1,43 @@ +/** + * + */ +package fr.u_paris.gla.project.utils; + +/** A utility class for computations related to GPS. + * + * @author Emmanuel Bigeon */ +public final class GPS { + + /** The (approximated) earth radius in km. */ + private static final double EARTH_RADIUS = 6_370.0; + + /** Hidden constructor for tool class */ + private GPS() { + // Tool class + } + + /** Convert a degree angle value in a radian angle one. + * + * @param degree the degree value + * @return the radian value */ + private static final double degreeToRadian(double degree) { + return degree / 180 * Math.PI; + } + + /** Compute the flying distance between two GPS positions. + * + * @param latitude1 the latitude of the first position + * @param longitude1 the longitude of the first position + * @param latitude2 the latitude of the second position + * @param longitude2 the longitude of the second position + * @return the flying distance */ + public static double distance(double latitude1, double longitude1, double latitude2, + double longitude2) { + double deltaLatitude = degreeToRadian(latitude2 - latitude1); + double deltaLongitude = degreeToRadian(longitude2 - longitude1); + double a = Math.pow(Math.sin(deltaLatitude / 2), 2) + + Math.pow(Math.sin(deltaLongitude), 2) * Math.cos(latitude1) + * Math.cos(latitude2); + return EARTH_RADIUS * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); + } +}