Skip to content

Commit 9484c7e

Browse files
Priyanshu1303dPriyanshu1303d
andauthored
FEAT(geometry): Add Haversine formula and fix build issues (#6650)
[FEAT] Implemented Haversine Formula Co-authored-by: Priyanshu1303d <[email protected]>
1 parent e6cb96f commit 9484c7e

File tree

4 files changed

+112
-7
lines changed

4 files changed

+112
-7
lines changed

pom.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@
7171
<artifactId>maven-compiler-plugin</artifactId>
7272
<version>3.14.1</version>
7373
<configuration>
74-
<source>21</source>
75-
<target>21</target>
74+
<release>21</release>
7675
<compilerArgs>
7776
<arg>-Xlint:all</arg>
7877
<arg>-Xlint:-auxiliaryclass</arg>

src/main/java/com/thealgorithms/dynamicprogramming/PartitionProblem.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
import java.util.Arrays;
13
/**
24
* @author Md Asif Joardar
35
*
@@ -13,11 +15,6 @@
1315
*
1416
* The time complexity of the solution is O(n × sum) and requires O(n × sum) space
1517
*/
16-
17-
package com.thealgorithms.dynamicprogramming;
18-
19-
import java.util.Arrays;
20-
2118
public final class PartitionProblem {
2219
private PartitionProblem() {
2320
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.thealgorithms.geometry;
2+
/**
3+
* This Class implements the Haversine formula to calculate the distance between two points on a sphere (like Earth) from their latitudes and longitudes.
4+
*
5+
* The Haversine formula is used in navigation and mapping to find the great-circle distance,
6+
* which is the shortest distance between two points along the surface of a sphere. It is often
7+
* used to calculate the "as the crow flies" distance between two geographical locations.
8+
*
9+
* The formula is reliable for all distances, including small ones, and avoids issues with
10+
* numerical instability that can affect other methods.
11+
*
12+
* @see "https://en.wikipedia.org/wiki/Haversine_formula" - Wikipedia
13+
*/
14+
public final class Haversine {
15+
16+
// Average radius of Earth in kilometers
17+
private static final double EARTH_RADIUS_KM = 6371.0;
18+
19+
/**
20+
* Private constructor to prevent instantiation of this utility class.
21+
*/
22+
private Haversine() {
23+
}
24+
25+
/**
26+
* Calculates the great-circle distance between two points on the earth
27+
* (specified in decimal degrees).
28+
*
29+
* @param lat1 Latitude of the first point in decimal degrees.
30+
* @param lon1 Longitude of the first point in decimal degrees.
31+
* @param lat2 Latitude of the second point in decimal degrees.
32+
* @param lon2 Longitude of the second point in decimal degrees.
33+
* @return The distance between the two points in kilometers.
34+
*/
35+
public static double haversine(double lat1, double lon1, double lat2, double lon2) {
36+
// Convert latitude and longitude from degrees to radians
37+
double dLat = Math.toRadians(lat2 - lat1);
38+
double dLon = Math.toRadians(lon2 - lon1);
39+
40+
double lat1Rad = Math.toRadians(lat1);
41+
double lat2Rad = Math.toRadians(lat2);
42+
43+
// Apply the Haversine formula
44+
double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1Rad) * Math.cos(lat2Rad);
45+
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
46+
47+
return EARTH_RADIUS_KM * c;
48+
}
49+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.thealgorithms.geometry;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
/**
12+
* Unit tests for the Haversine formula implementation.
13+
* This class uses parameterized tests to verify the distance calculation
14+
* between various geographical coordinates.
15+
*/
16+
final class HaversineTest {
17+
18+
// A small tolerance for comparing double values, since floating-point
19+
// arithmetic is not always exact. A 1km tolerance is reasonable for these distances.
20+
private static final double DELTA = 1.0;
21+
22+
/**
23+
* Provides test cases for the haversine distance calculation.
24+
* Each argument contains: lat1, lon1, lat2, lon2, and the expected distance in kilometers.
25+
*
26+
* @return a stream of arguments for the parameterized test.
27+
*/
28+
static Stream<Arguments> haversineTestProvider() {
29+
return Stream.of(
30+
// Case 1: Distance between Paris, France and Tokyo, Japan
31+
Arguments.of(48.8566, 2.3522, 35.6895, 139.6917, 9712.0),
32+
33+
// Case 2: Distance between New York, USA and London, UK
34+
Arguments.of(40.7128, -74.0060, 51.5074, -0.1278, 5570.0),
35+
36+
// Case 3: Zero distance (same point)
37+
Arguments.of(52.5200, 13.4050, 52.5200, 13.4050, 0.0),
38+
39+
// Case 4: Antipodal points (opposite sides of the Earth)
40+
// Should be approximately half the Earth's circumference (PI * radius)
41+
Arguments.of(0.0, 0.0, 0.0, 180.0, 20015.0));
42+
}
43+
44+
/**
45+
* Tests the haversine method with various sets of coordinates.
46+
*
47+
* @param lat1 Latitude of the first point.
48+
* @param lon1 Longitude of the first point.
49+
* @param lat2 Latitude of the second point.
50+
* @param lon2 Longitude of the second point.
51+
* @param expectedDistance The expected distance in kilometers.
52+
*/
53+
@ParameterizedTest
54+
@MethodSource("haversineTestProvider")
55+
@DisplayName("Test Haversine distance calculation for various coordinates")
56+
void testHaversine(double lat1, double lon1, double lat2, double lon2, double expectedDistance) {
57+
double actualDistance = Haversine.haversine(lat1, lon1, lat2, lon2);
58+
assertEquals(expectedDistance, actualDistance, DELTA);
59+
}
60+
}

0 commit comments

Comments
 (0)