From 9bb8f1b52448dd94eec2dac4c51f9408f063761b Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 17 May 2002 21:12:14 +0000 Subject: [PATCH] Moved routines to calculate orientation from point a toward point b into DirectionUtil. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1373 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/util/DirectionUtil.java | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) 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",