jrobots.utils
Class Vector

java.lang.Object
  extended by jrobots.utils.Vector
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class Vector
extends java.lang.Object
implements java.io.Serializable, java.lang.Cloneable

This class represents a two dimensional vector.

The internal fields are immutable - they are set once at the construction of the vector. All methods returning a vector construct a new vector.

The vector can be treated both in Cartesian and in polar form. Like below:

See Also:
getAngle(), getLength(), add(Vector), sub(Vector), mult(double), getX(), getY(), ZERO, rotate(Angle), projection(Angle), vectorProduct(Vector), Serialized Form

Field Summary
static Vector ZERO
           
 
Constructor Summary
Vector()
          Constructs the zero vector.
Vector(Angle angle, double length)
          Constructs a vector by polar coordinates.
Vector(double length, Angle angle)
          Constructs a vector by polar coordinates.
Vector(double x, double y)
          Constructs a vector by Cartesian coordinates (x,y).
 
Method Summary
 Vector add(Vector v)
          Addition of two vectors.
 Vector clone()
           
 Vector collisionPointBetweenEllipses(Vector ellipseDimensionsOwn, Angle orientationOwn, Vector posOther, Vector ellipseDimensionsOther, Angle orientationOther)
          Determines the collision surface normal from the rotated ellipse given by this Vector and the other given rotated ellipse.
 double distanceTo(Vector b)
          Calculates the distance between two vectors.
 boolean equals(java.lang.Object obj)
           
 Angle getAngle()
          Gets the polar angle of this vector.
 Angle getEllipseSurfaceNormal(Angle axisThroughCenter)
          Calculates the surface normal for the ellipse given by this {@link Vector
 double getLength()
          Returns the length of this vector
 Vector getNegative()
          The inverse vector has same length and opposite direction.
 Vector getNormal()
          Scales this vector to a length of 1.
 double getWeightedDiameterOfEllipse(Angle localAxesDir)
          treat this vector as the dimensions of an ellipse and calculate the projected diameter.
 double getX()
          Returns the Cartesian x component.
 double getY()
          Returns the Cartesian y component.
 int hashCode()
           
 boolean insideEllipse(Vector ellipse, Vector ellipseCenter, Angle ellipseOrientation)
          Determines if this point (global coords) is inside an ellipse.
 Vector mult(double factor)
          Scales this vector by a given factor.
 double projection(Angle angle)
          Projects the vector onto a given Angle.
 Vector rotate(Angle angle)
          Rotates a vector in clockwise direction.
 Vector sub(Vector v)
          Subtracts a given vector from this vector.
 java.lang.String toString()
           
 double vectorProduct(Vector b)
          The vector product in the two dimensional space results in a scalar value.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final Vector ZERO
Constructor Detail

Vector

public Vector()
Constructs the zero vector.


Vector

public Vector(Angle angle,
              double length)
Constructs a vector by polar coordinates.

Parameters:
angle -
length -

Vector

public Vector(double length,
              Angle angle)
Constructs a vector by polar coordinates.

Parameters:
length -
angle -

Vector

public Vector(double x,
              double y)
Constructs a vector by Cartesian coordinates (x,y).

Parameters:
x - double
y - double
Method Detail

add

public Vector add(Vector v)
Addition of two vectors. Result is a new vector.

Parameters:
v - Summand
Returns:
sum as new vector

clone

public Vector clone()
Overrides:
clone in class java.lang.Object

collisionPointBetweenEllipses

public Vector collisionPointBetweenEllipses(Vector ellipseDimensionsOwn,
                                            Angle orientationOwn,
                                            Vector posOther,
                                            Vector ellipseDimensionsOther,
                                            Angle orientationOther)
Determines the collision surface normal from the rotated ellipse given by this Vector and the other given rotated ellipse.

Parameters:
ellipseDimensionsOwn - - length and breadth of this ellipse; [m]; local axes
orientationOwn -
posOther -
ellipseDimensionsOther - - length and breadth of other ellipse; [m]; local axes
orientationOther -
Returns:
local coords collision point from given own ellipse; null iff there is no collision

distanceTo

public double distanceTo(Vector b)
Calculates the distance between two vectors.

Note that the vectors must share the same reference system.

Parameters:
b - Vector
Returns:
|this-b|

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

getAngle

public Angle getAngle()
Gets the polar angle of this vector. Zero vectors point east.

Returns:
Angle

getEllipseSurfaceNormal

public Angle getEllipseSurfaceNormal(Angle axisThroughCenter)
Calculates the surface normal for the ellipse given by this {@link Vector

Parameters:
axisThroughCenter - - local axes
Returns:
surface normal; local axes

getLength

public double getLength()
Returns the length of this vector

Returns:
length

getNegative

public Vector getNegative()
The inverse vector has same length and opposite direction.

Returns:
new inverse vector

getNormal

public Vector getNormal()
Scales this vector to a length of 1. Zero vectors point east.

Returns:
new normalized vector

getWeightedDiameterOfEllipse

public double getWeightedDiameterOfEllipse(Angle localAxesDir)
treat this vector as the dimensions of an ellipse and calculate the projected diameter.

Parameters:
localAxesDir - - local axes
Returns:
diameter at given projection angle

getX

public double getX()
Returns the Cartesian x component.

Returns:
double

getY

public double getY()
Returns the Cartesian y component.

Returns:
double

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

insideEllipse

public boolean insideEllipse(Vector ellipse,
                             Vector ellipseCenter,
                             Angle ellipseOrientation)
Determines if this point (global coords) is inside an ellipse.

Parameters:
ellipse - - local coords; x is length, y is breadth
ellipseCenter - - global coords
Returns:
false iff the given point is outside of the ellipse

mult

public Vector mult(double factor)
Scales this vector by a given factor.

Parameters:
factor -
Returns:
product as new vector

projection

public double projection(Angle angle)
Projects the vector onto a given Angle. The result is the projected length.

Parameters:
angle - - projection angle
Returns:
length of vector in the given direction

rotate

public Vector rotate(Angle angle)
Rotates a vector in clockwise direction. The result is a new vector. Length remains and direction is rotated by the given angle.

Parameters:
angle - rotation angle (>0 for clockwise rotation)
Returns:
new rotated vector.

sub

public Vector sub(Vector v)
Subtracts a given vector from this vector. Result is a new vector.

Parameters:
v - Subtrahend
Returns:
difference as new vector

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

vectorProduct

public double vectorProduct(Vector b)
The vector product in the two dimensional space results in a scalar value. The mathematical representation of a.vectorProduct(b) is a x b.

Parameters:
b - - second parameter of the vector product
Returns:
scalar value