| | |
| | | Integer integer = LatelyAndLessThan(devpPosition, nowPosition,perimeter); |
| | | for (BasDevpPosition basDevpPosition:devpPosition){ |
| | | if (basDevpPosition.getDevNo().equals(integer)){ |
| | | if (basDevpPosition.getDevNo() == 133){ |
| | | if (basDevpPosition.getDevNo() == 134){ |
| | | result = 101; |
| | | } |
| | | break; |
| | | } |
| | | result = basDevpPosition.getDevNo(); |
| | | } |
| | | return result; |
| | | return result == -1? 101 : result; |
| | | } |
| | | // //获取最近并在当前位置前边的位置 |
| | | // public static Long LatelyAndLessThan(long[] devpPosition,long nowPosition){ |
| | |
| | | public static Double LatelyAndLessThan(long devPosition,long rgvPosition,long perimeter){ |
| | | long Difference = perimeter; |
| | | if (devPosition >= rgvPosition){ |
| | | if ((devPosition-rgvPosition) < Difference){ |
| | | // if ((devPosition-rgvPosition) < Difference){ |
| | | Difference = devPosition-rgvPosition; |
| | | } |
| | | // } |
| | | } else { |
| | | if (perimeter - (rgvPosition - devPosition) < Difference){ |
| | | // if (perimeter - (rgvPosition - devPosition) < Difference){ |
| | | Difference = perimeter - (rgvPosition - devPosition); |
| | | } |
| | | // } |
| | | } |
| | | return Double.valueOf(Difference); |
| | | } |
| | | |
| | | public static int calculateShortestDistanceDistance(List<Integer> data, int a, int b) { |
| | | Result result = calculateShortestDistance(data, a, b); |
| | | return result.distance; |
| | | } |
| | | |
| | | public static List<Integer> calculateShortestDistancePassedData(List<Integer> data, int a, int b) { |
| | | Result result = calculateShortestDistance(data, a, b); |
| | | return result.passedData; |
| | | } |
| | | |
| | | // public static String calculateShortestDistanceDirection(List<Integer> data, int a, int b) { |
| | | // Result result = calculateShortestDistance(data, a, b); |
| | | // return result.direction; |
| | | // } |
| | | |
| | | public static boolean calculateShortestDistanceDirection(List<Integer> data, int a, int b) { |
| | | Result result = calculateShortestDistance(data, a, b); |
| | | return result.direction.equals("向前走(顺时针)"); |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | List<Integer> data = new ArrayList<>(); |
| | | // 用 List<Integer> 初始化数据 |
| | | for (int i = 1; i <= 9; i++) { |
| | | data.add(i); |
| | | } |
| | | |
| | | int a = 3; // 起点 |
| | | int b = 3; // 终点 |
| | | |
| | | Result result = calculateShortestDistance(data, a, b); |
| | | System.out.println("最近的距离是: " + result.distance); |
| | | System.out.println("经过的数据: " + result.passedData); |
| | | System.out.println("方向: " + result.direction); |
| | | } |
| | | |
| | | public static Result calculateShortestDistance(List<Integer> data, int a, int b) { |
| | | int length = data.size(); |
| | | int indexA = data.indexOf(a); |
| | | int indexB = data.indexOf(b); |
| | | |
| | | if (indexA == -1 || indexB == -1) { |
| | | throw new IllegalArgumentException("列表中未找到指定的元素"); |
| | | } |
| | | |
| | | // 顺时针方向的距离及经过的数据 |
| | | List<Integer> clockwiseData = new ArrayList<>(); |
| | | int clockwiseDistance = (indexB - indexA + length) % length; |
| | | for (int i = 0; i <= clockwiseDistance; i++) { |
| | | clockwiseData.add(data.get((indexA + i) % length)); |
| | | } |
| | | |
| | | // 逆时针方向的距离及经过的数据 |
| | | List<Integer> counterClockwiseData = new ArrayList<>(); |
| | | int counterClockwiseDistance = (indexA - indexB + length) % length; |
| | | for (int i = 0; i <= counterClockwiseDistance; i++) { |
| | | counterClockwiseData.add(data.get((indexA - i + length) % length)); |
| | | } |
| | | |
| | | // 比较距离并返回结果 |
| | | if (clockwiseDistance <= counterClockwiseDistance) { |
| | | return new Result(clockwiseDistance, clockwiseData, "向前走(顺时针)"); |
| | | } else { |
| | | return new Result(counterClockwiseDistance, counterClockwiseData, "向后走(逆时针)"); |
| | | } |
| | | } |
| | | |
| | | // 用于返回结果的类 |
| | | static class Result { |
| | | public int distance; |
| | | public List<Integer> passedData; |
| | | public String direction; |
| | | |
| | | public Result(int distance, List<Integer> passedData, String direction) { |
| | | this.distance = distance; |
| | | this.passedData = passedData; |
| | | this.direction = direction; |
| | | } |
| | | } |
| | | |
| | | } |