Class Direction

java.lang.Object
  |
  +--Direction

public class Direction
extends Object

A Direction object represents a direction in 2-dimensional space. The angle of a direction may be obtained by using either the getDegrees() method or the getRadians() method. The angle is measured anti-clockwise from the East direction . For example, 0 degrees indicates a direction of East, 90 degrees indicates North,

A new Direction object may be created by specifying the degrees as a parameter to the constructor. In addition, common Directions have been defined as static members of this class. For example:

Direction.NORTH
may be used instead of
new Direction(90.0)

Author:
Ryan Heise

Field Summary
static Direction EAST
          A predefined Direction representing East.
static Direction NORTH
          A predefined Direction representing North.
static Direction NORTH_EAST
          A predefined Direction representing Northeast.
static Direction NORTH_WEST
          A predefined Direction representing Northwest.
static Direction SOUTH
          A predefined Direction representing South.
static Direction SOUTH_EAST
          A predefined Direction representing Southeast.
static Direction SOUTH_WEST
          A predefined Direction representing Southwest.
static Direction WEST
          A predefined Direction representing West.
 
Constructor Summary
Direction(double degrees)
          Creates a new Direction object, specified in degrees.
 
Method Summary
 double getDegrees()
          Gets the angle of this Direction in degrees.
 Direction getOppositeDirection()
          Returns a new direction object that points in the opposite direction to this one.
 double getRadians()
          Gets the angle of this Direction in radians.
 Direction getRelativeDirection(double degrees)
          Gets a new direction relative to this direction by adding the number of degrees specified.
 String toString()
          Returns all information about an instance of a Direction as a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

EAST

public static final Direction EAST
A predefined Direction representing East.


NORTH

public static final Direction NORTH
A predefined Direction representing North.


NORTH_EAST

public static final Direction NORTH_EAST
A predefined Direction representing Northeast.


NORTH_WEST

public static final Direction NORTH_WEST
A predefined Direction representing Northwest.


SOUTH

public static final Direction SOUTH
A predefined Direction representing South.


SOUTH_EAST

public static final Direction SOUTH_EAST
A predefined Direction representing Southeast.


SOUTH_WEST

public static final Direction SOUTH_WEST
A predefined Direction representing Southwest.


WEST

public static final Direction WEST
A predefined Direction representing West.

Constructor Detail

Direction

public Direction(double degrees)
Creates a new Direction object, specified in degrees.

Parameters:
degrees - the angle of this direction, specified in degrees.
Method Detail

getDegrees

public double getDegrees()
Gets the angle of this Direction in degrees.

Returns:
the angle of this Direction in degrees.

getOppositeDirection

public Direction getOppositeDirection()
Returns a new direction object that points in the opposite direction to this one.

Returns:
the opposite direction of this direction.

getRadians

public double getRadians()
Gets the angle of this Direction in radians.

Returns:
the angle of this Direction in radians.

getRelativeDirection

public Direction getRelativeDirection(double degrees)
Gets a new direction relative to this direction by adding the number of degrees specified. For example:
 Direction north = new Direction(90.0);
 Direction newDirection = north.getRelativeDirection(20.0);
 
newDirection.getDegrees() should return 110.0.

Parameters:
degrees - specifies how many degrees should be added to produce the new direction.
Returns:
the new relative direction.

toString

public String toString()
Returns all information about an instance of a Direction as a string.

It can be very useful to have a "toString" method in any class you write. It allows you to use a reference to an object of that class as if that object is a string. For example, you can use the name of the object in some sort of an output, as if the object is a string, and the Java runtime environment will use your "toString" to produce a real String.

Overrides:
toString in class Object
Returns:
all information about the Direction object as a string.


Generated by BlueJ