程序使用了HashSet来存储第一个路径的所有经纬度坐标,然后遍历第二个路径,如果路径点也在HashSet中,则计数器加1。在存储坐标时,使用了roundCoordinates函数来对经纬度进行四舍五入,以保证在指定的容差范围内(tolerance)两个坐标被认为是相同的。最后,将重合的坐标点数除以两个路径中最小的长度,然后乘以100,得出重合度的百分比。
import java.util.HashSet;
import java.util.Set;
public class LocationPathOverlap {
public static double calculateOverlap(double[][] path1, double[][] path2, double tolerance) {
Set<String> set = new HashSet<>();
int overlapCount = 0;
for (int i = 0; i < path1.length; i++) {
set.add(roundCoordinates(path1[i][0], path1[i][1], tolerance));
}
for (int i = 0; i < path2.length; i++) {
if (set.contains(roundCoordinates(path2[i][0], path2[i][1], tolerance))) {
overlapCount++;
}
}
double overlapPercent = (double)overlapCount / Math.min(path1.length, path2.length) * 100;
return overlapPercent;
}
public static String roundCoordinates(double latitude, double longitude, double tolerance) {
double latRound = Math.round(latitude / tolerance) * tolerance;
double longRound = Math.round(longitude / tolerance) * tolerance;
return latRound + "," + longRound;
}
public static void main(String[] args) {
double[][] path1 = {{37.7749, -122.4194}, {37.7750, -122.4188}, {37.7751, -122.4177}, {37.7752, -122.4166}, {37.7753, -122.4155}, {37.7754, -122.4144}};
double[][] path2 = {{37.7749, -122.4194}, {37.7751, -122.4177}, {37.7753, -122.4155}, {37.7755, -122.4133}, {37.7757, -122.4111}};
double overlapPercent = calculateOverlap(path1, path2, 0.001);
System.out.println("Overlap percent: " + overlapPercent + "%");
}
}