Various and sundry modifications to clean up orientation handling and add

support for 16 orientations in situations where it is merited.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1548 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-06-26 23:53:07 +00:00
parent d1b428158a
commit e75ae89f22
11 changed files with 338 additions and 32 deletions
@@ -1,5 +1,5 @@
//
// $Id: DirectionUtil.java,v 1.5 2002/06/26 02:54:56 mdb Exp $
// $Id: DirectionUtil.java,v 1.6 2002/06/26 23:53:07 mdb Exp $
package com.threerings.util;
@@ -37,6 +37,36 @@ public class DirectionUtil implements DirectionCodes
SHORT_DIR_STRINGS[direction] : "?";
}
/**
* Returns the direction code that corresponds to the supplied string
* or {@link NONE} if the string does not correspond to a known
* direction code.
*/
public static int fromString (String dirstr)
{
for (int ii = 0; ii < FINE_DIRECTION_COUNT; ii++) {
if (DIR_STRINGS[ii].equals(dirstr)) {
return ii;
}
}
return NONE;
}
/**
* Returns the direction code that corresponds to the supplied short
* string or {@link NONE} if the string does not correspond to a known
* direction code.
*/
public static int fromShortString (String dirstr)
{
for (int ii = 0; ii < FINE_DIRECTION_COUNT; ii++) {
if (SHORT_DIR_STRINGS[ii].equals(dirstr)) {
return ii;
}
}
return NONE;
}
/**
* Returns a string representation of an array of direction codes. The
* directions are represented by the abbreviated names.
@@ -100,7 +130,19 @@ public class DirectionUtil implements DirectionCodes
*/
public static int getDirection (int ax, int ay, int bx, int by)
{
double theta = Math.atan2(by-ay, bx-ax);
return getDirection(Math.atan2(by-ay, bx-ax));
}
/**
* Returns which of the eight compass directions is associated with
* the specified angle theta. <em>Note:</em> that the angle supplied
* is assumed to increase clockwise around the origin (which screen
* angles do) rather than counter-clockwise around the origin (which
* cartesian angles do) and <code>NORTH</code> is considered to point
* toward the top of the screen.
*/
public static int getDirection (double theta)
{
theta = ((theta + Math.PI) * 4) / Math.PI;
return (int)(Math.round(theta) + WEST) % 8;
}
@@ -128,7 +170,19 @@ public class DirectionUtil implements DirectionCodes
*/
public static int getFineDirection (int ax, int ay, int bx, int by)
{
double theta = Math.atan2(by-ay, bx-ax);
return getFineDirection(Math.atan2(by-ay, bx-ax));
}
/**
* Returns which of the sixteen compass directions is associated with
* the specified angle theta. <em>Note:</em> that the angle supplied
* is assumed to increase clockwise around the origin (which screen
* angles do) rather than counter-clockwise around the origin (which
* cartesian angles do) and <code>NORTH</code> is considered to point
* toward the top of the screen.
*/
public static int getFineDirection (double theta)
{
theta = ((theta + Math.PI) * 8) / Math.PI;
return ANGLE_MAP[(int)Math.round(theta) % FINE_DIRECTION_COUNT];
}