diff --git a/src/java/com/threerings/util/DirectionUtil.java b/src/java/com/threerings/util/DirectionUtil.java index 11947beb4..a56c318e0 100644 --- a/src/java/com/threerings/util/DirectionUtil.java +++ b/src/java/com/threerings/util/DirectionUtil.java @@ -1,8 +1,10 @@ // -// $Id: DirectionUtil.java,v 1.3 2001/12/17 03:51:35 mdb Exp $ +// $Id: DirectionUtil.java,v 1.4 2002/05/17 21:12:14 mdb Exp $ package com.threerings.util; +import java.awt.Point; + /** * Direction related utility functions. */ @@ -51,6 +53,47 @@ public class DirectionUtil implements DirectionCodes return buf.append("}").toString(); } + /** + * Returns the direction that point b lies in from point + * a as one of the {@link DirectionCodes} direction + * constants. + */ + public static int getDirection (Point a, Point b) + { + return getDirection(a.x, a.y, b.x, b.y); + } + + /** + * Returns the direction that point b lies in from point + * a as one of the {@link DirectionCodes} direction + * constants. + */ + public static int getDirection (int ax, int ay, int bx, int by) + { + if (ax == bx && ay > by) { + return NORTH; + } else if (ax == bx && ay < by) { + return SOUTH; + + } else if (ax < bx && ay > by) { + return NORTHEAST; + } else if (ax < bx && ay == by) { + return EAST; + } else if (ax < bx && ay < by) { + return SOUTHEAST; + + } else if (ax > bx && ay < by) { + return SOUTHWEST; + } else if (ax > bx && ay == by) { + return WEST; + } else if (ax > bx && ay > by) { + return NORTHWEST; + + } else { + return NONE; + } + } + /** Direction constant string names. */ protected static final String[] DIR_STRINGS = { "SOUTHWEST", "WEST", "NORTHWEST", "NORTH",