diff --git a/src/java/com/threerings/util/DirectionUtil.java b/src/java/com/threerings/util/DirectionUtil.java
index 79b23b922..99254300f 100644
--- a/src/java/com/threerings/util/DirectionUtil.java
+++ b/src/java/com/threerings/util/DirectionUtil.java
@@ -23,6 +23,8 @@ package com.threerings.util;
import java.awt.Point;
+import com.samskivert.util.IntListUtil;
+
/**
* Direction related utility functions.
*/
@@ -133,6 +135,44 @@ public class DirectionUtil implements DirectionCodes
return rotateCW(direction, FINE_CW_ROTATE.length/2);
}
+ /**
+ * Get the direction closest to the specified direction, out of
+ * the directions in the possible list (preferring a clockwise match).
+ */
+ public static int getClosest (int direction, int[] possible)
+ {
+ return getClosest(direction, possible, true);
+ }
+
+ /**
+ * Get the direction closest to the specified direction, out of
+ * the directions in the possible list.
+ *
+ * @param preferCW whether to prefer a clockwise match or a
+ * counter-clockwise match.
+ */
+ public static int getClosest (int direction, int[] possible,
+ boolean preferCW)
+ {
+ // rotate a tick at a time, looking for matches
+ int first = direction;
+ int second = direction;
+ for (int ii = 0; ii <= FINE_DIRECTION_COUNT / 2; ii++) {
+ if (IntListUtil.contains(possible, first)) {
+ return first;
+ }
+
+ if (ii != 0 && IntListUtil.contains(possible, second)) {
+ return second;
+ }
+
+ first = preferCW ? rotateCW(first, 1) : rotateCCW(first, 1);
+ second = preferCW ? rotateCCW(second, 1) : rotateCW(second, 1);
+ }
+
+ return NONE;
+ }
+
/**
* Returns which of the eight compass directions that point
* b lies in from point a as one of the