package com.zy.asrs.utils;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
public class TrackUtils {
|
|
public static void main(String[] args) {
|
// 模拟数据
|
ArrayList<Long[]> arrayListAll = new ArrayList<>();
|
arrayListAll.add(new Long[]{999L, 100L, 500L}); // 方向: 100→500
|
// arrayListAll.add(new Long[]{998L, 600L, 400L}); // 方向: 600→400 (反向)
|
// arrayListAll.add(new Long[]{1000L, 400L, 700L}); // 方向: 600→400 (反向)
|
// arrayListAll.add(new Long[]{996L, 10L, 50L}); // 方向: 100→500
|
// arrayListAll.add(new Long[]{995L, 15L, 60L}); // 方向: 150→600
|
|
// 找出所有交叉的数据对
|
List<CrossPair> crossPairs = findCrossingPaths(arrayListAll);
|
|
// 打印结果
|
System.out.println("交叉的路径对:");
|
crossPairs.forEach(pair ->
|
System.out.println(pair.path1[0] + " (" + pair.path1[1] + "→" + pair.path1[2] +
|
") 与 " + pair.path2[0] + " (" + pair.path2[1] + "→" + pair.path2[2] + ") 交叉"));
|
}
|
|
/**
|
* 找出所有交叉的路径对
|
* 交叉条件:
|
* 1. 方向不同 (起点<终点 vs 起点>终点)
|
* 2. 路径有重叠
|
*/
|
public static List<Long[]> findCrossingPathsW(ArrayList<Long[]> paths) {
|
if (paths == null || paths.size() <2){
|
return new ArrayList<>();
|
}
|
List<CrossPair> crossPairs = findCrossingPaths(paths);
|
List<Long[]> arrayListAll = new ArrayList<>();
|
for (CrossPair crossPair : crossPairs) {
|
arrayListAll.add(new Long[]{crossPair.path1[0], crossPair.path1[1]});
|
}
|
return arrayListAll;
|
}
|
public static List<CrossPair> findCrossingPaths(ArrayList<Long[]> paths) {
|
List<CrossPair> crossPairs = new ArrayList<>();
|
|
// 获取所有路径
|
List<Long[]> pathList = new ArrayList<>(paths);
|
|
// 使用Java 8 Stream API比较所有路径对
|
crossPairs = pathList.stream()
|
.flatMap(path1 ->
|
pathList.stream()
|
.filter(path2 -> !path1[0].equals(path2[0])) // 排除自身
|
.filter(path2 -> isOppositeDirection(path1, path2)) // 方向不同
|
.filter(path2 -> isOverlapping(path1, path2)) // 有重叠
|
.map(path2 -> new CrossPair(path1, path2))
|
)
|
.distinct() // 去重 (A-B 和 B-A 视为同一对)
|
.collect(Collectors.toList());
|
|
return crossPairs;
|
}
|
|
/**
|
* 判断两条路径方向是否相反
|
*/
|
private static boolean isOppositeDirection(Long[] path1, Long[] path2) {
|
long start1 = path1[1];
|
long end1 = path1[2];
|
long start2 = path2[1];
|
long end2 = path2[2];
|
|
// 判断方向:true表示正向(start<end),false表示反向(start>end)
|
boolean direction1 = start1 < end1;
|
boolean direction2 = start2 < end2;
|
|
return direction1 != direction2;
|
}
|
|
/**
|
* 判断两条路径是否有重叠
|
*/
|
private static boolean isOverlapping(Long[] path1, Long[] path2) {
|
long start1 = Math.min(path1[1], path1[2]); // 实际起点(较小值)
|
long end1 = Math.max(path1[1], path1[2]); // 实际终点(较大值)
|
long start2 = Math.min(path2[1], path2[2]);
|
long end2 = Math.max(path2[1], path2[2]);
|
|
// 检查区间是否有重叠
|
return !(end1 <= start2 || end2 <= start1);
|
}
|
|
/**
|
* 辅助类,用于表示交叉的路径对
|
*/
|
static class CrossPair {
|
Long[] path1;
|
Long[] path2;
|
|
CrossPair(Long[] path1, Long[] path2) {
|
// 保证存储顺序一致,便于去重
|
if (path1[0] < path2[0]) {
|
this.path1 = path1;
|
this.path2 = path2;
|
} else {
|
this.path1 = path2;
|
this.path2 = path1;
|
}
|
}
|
|
@Override
|
public boolean equals(Object obj) {
|
if (this == obj) return true;
|
if (!(obj instanceof CrossPair)) return false;
|
CrossPair other = (CrossPair) obj;
|
return this.path1[0].equals(other.path1[0]) &&
|
this.path2[0].equals(other.path2[0]);
|
}
|
|
@Override
|
public int hashCode() {
|
return 31 * path1[0].hashCode() + path2[0].hashCode();
|
}
|
}
|
}
|