package com.zy.asrs.utils;
|
|
/**
|
* 距离优先级工具:
|
* 1) 距离 <= 阈值: 优先级 [0,1], 越近越高
|
* 2) 距离 > 阈值: 优先级 [0,-1], 越远越低
|
*/
|
public final class DistancePriorityUtil {
|
|
private DistancePriorityUtil() {
|
}
|
|
public static double calculate(long distance, long threshold, long perimeter) {
|
if (threshold <= 0L || perimeter <= 0L) {
|
return 0D;
|
}
|
long d = Math.max(0L, distance);
|
if (d <= threshold) {
|
double score = 1D - ((double) d / (double) threshold);
|
return clamp(score, 0D, 1D);
|
}
|
long outside = d - threshold;
|
long outsideMax = Math.max(1L, perimeter - threshold);
|
double score = -((double) outside / (double) outsideMax);
|
return clamp(score, -1D, 0D);
|
}
|
|
public static boolean isInRange(long distance, long threshold) {
|
return threshold > 0L && distance <= threshold;
|
}
|
|
private static double clamp(double value, double min, double max) {
|
if (value < min) {
|
return min;
|
}
|
if (value > max) {
|
return max;
|
}
|
return value;
|
}
|
}
|