Class Vector

java.lang.Object
  extended by Vector

public final class Vector
extends java.lang.Object

A 2D vector. The vector can be read and manipulated in Cartesian coordinates (as an x,y-offset pair) or in polar coordinates (as a direction and a length).

Version:
2.0
Author:
Poul Henriksen, Michael Kolling

Constructor Summary
Vector()
          Create a new, neutral vector.
Vector(double dx, double dy)
          Create a vector by specifying the x and y offsets from start to end points.
Vector(int direction, double length)
          Create a vector with given direction and length.
 
Method Summary
 void add(Vector other)
          Add another vector to this vector.
 int getDirection()
          Return the direction of this vector (in degrees).
 double getLength()
          Return the length of this vector.
 double getX()
          Return the x offset of this vector (start to end point).
 double getY()
          Return the y offset of this vector (start to end point).
 void revertHorizontal()
          Revert to horizontal component of this movement vector.
 void revertVertical()
          Revert to vertical component of this movement vector.
 void scale(double factor)
          Scale this vector up (factor greater than 1) or down (factor less than 1).
 void setDirection(int direction)
          Set the direction of this vector, leaving the length intact.
 void setLength(double length)
          Set the length of this vector, leaving the direction intact.
 void setNeutral()
          Set this vector to the neutral vector (length 0).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Vector

public Vector()
Create a new, neutral vector.


Vector

public Vector(double dx,
              double dy)
Create a vector by specifying the x and y offsets from start to end points.


Vector

public Vector(int direction,
              double length)
Create a vector with given direction and length. The direction should be in the range [0..359], where 0 is EAST, and degrees increase clockwise.

Method Detail

add

public void add(Vector other)
Add another vector to this vector.


getDirection

public int getDirection()
Return the direction of this vector (in degrees). 0 is EAST.


getLength

public double getLength()
Return the length of this vector.


getX

public double getX()
Return the x offset of this vector (start to end point).


getY

public double getY()
Return the y offset of this vector (start to end point).


revertHorizontal

public void revertHorizontal()
Revert to horizontal component of this movement vector.


revertVertical

public void revertVertical()
Revert to vertical component of this movement vector.


scale

public void scale(double factor)
Scale this vector up (factor greater than 1) or down (factor less than 1). The direction remains unchanged.


setDirection

public void setDirection(int direction)
Set the direction of this vector, leaving the length intact.


setLength

public void setLength(double length)
Set the length of this vector, leaving the direction intact.


setNeutral

public void setNeutral()
Set this vector to the neutral vector (length 0).