com.planeth.common.math
Class Vector2D

java.lang.Object
  extended bycom.planeth.common.math.Vector2D
All Implemented Interfaces:
java.lang.Cloneable

public class Vector2D
extends java.lang.Object
implements java.lang.Cloneable

2D Vector implementation

Author:
Andreas Grässer, gal@planet-h.com.
created May 15, 2005

Field Summary
static Vector2D ZERO_VECTOR
           
 
Constructor Summary
Vector2D(double size)
          Constructor for Vector2D.
Vector2D(double x, double y)
          Constructor for Vector2D.
 
Method Summary
 Vector2D addWith(Vector2D vector)
          Adds this Vector with the passed Vector.
static Vector2D avg(Vector2D[] vertexes)
          Returns the average Vector of the passed vertexes
 double avgDistance2VectorArray(Vector2D[] vertexes)
          Returns the average distance between this Vector and the passed array of Vectors.
 java.lang.Object clone()
          Returns a clone of this Vector
static Vector2D createVectorByAngleAndMagnitude(double theta, double length)
          Erstellt einen Vektor anhand seines Richtungswinkels und seiner Distanz Creates a new Vector2D by angle and magnitude
 double crossProduct(Vector2D vector)
          Returns the cross product of this Vector and the passed Vector.
 double distance2Vector(Vector2D vector)
          Returns the distance between this Vector and the passed Vector.
 Vector2D divideWith(double value)
          Divides this Vector with the value.
 double dotProduct(Vector2D vector)
          Returns the dot product of this Vector and the passed Vector.
 boolean equals(java.lang.Object obj)
          Returns true, if the coordinates of the Vector equals with the coordinates of the passed Vector (Object).
 double getAngle()
          Returns the angle (in radians) of the Vector.
 int getIntX()
          Returns the int value for x
 int getIntY()
          Returns the int value for y
 double getMagnitude()
          Returns the magnitude (length) of the Vector.
 double getX()
           
 double getY()
           
 boolean isZeroVector()
          Returns true, if the Vector is (0,0)
 double maxDistance2VectorArray(Vector2D[] vectorArray)
          Returns the max distance between this Vector and the passed array of Vectors.
 Vector2D multipyWith(double value)
          Multiplies this Vector with the passed value.
 Vector2D normalize()
          Normalizes the vector to maintain the same direction but with a absolute.
 void setLocation(double x, double y)
          Sets the coordinates of the Vector to a new position
 Vector2D subtractWith(Vector2D vector)
          Subtracts this Vector with the passed Vector.
static Vector2D sum(Vector2D[] vertexes)
          Returns the Vector sum of the passed vertexes
 java.lang.String toString()
          Returns the String interpretation of the Vector
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO_VECTOR

public static final Vector2D ZERO_VECTOR
Constructor Detail

Vector2D

public Vector2D(double size)
Constructor for Vector2D.
Creates a Vector2D with the same size at both coordinates.

Parameters:
size - value for x and y coordinate

Vector2D

public Vector2D(double x,
                double y)
Constructor for Vector2D.

Parameters:
x - the x coordinate
y - the y coordinate
Method Detail

sum

public static Vector2D sum(Vector2D[] vertexes)
Returns the Vector sum of the passed vertexes

Parameters:
vertexes - array of Vector2D
Returns:
Returns the Vector sum of the passed vertexes

avg

public static Vector2D avg(Vector2D[] vertexes)
Returns the average Vector of the passed vertexes

Parameters:
vertexes - array of Vector2D
Returns:
Returns the average Vector of the passed vertexes

createVectorByAngleAndMagnitude

public static Vector2D createVectorByAngleAndMagnitude(double theta,
                                                       double length)
Erstellt einen Vektor anhand seines Richtungswinkels und seiner Distanz Creates a new Vector2D by angle and magnitude

Parameters:
theta - angle in radians
length - magnitude of the Vector
Returns:
Vector2D

isZeroVector

public boolean isZeroVector()
Returns true, if the Vector is (0,0)

Returns:
Returns true, if the Vector is (0,0)

getAngle

public double getAngle()
Returns the angle (in radians) of the Vector.
The ZeroAngle is horizontal on the right side.

Returns:
angle in radians

getMagnitude

public double getMagnitude()
Returns the magnitude (length) of the Vector.
The result is always a positive double value

Returns:
the magnitude (length) of the Vector

addWith

public Vector2D addWith(Vector2D vector)
Adds this Vector with the passed Vector. The Result is a new Vector instance.

Parameters:
vector - Vector to add
Returns:
Vector2D

subtractWith

public Vector2D subtractWith(Vector2D vector)
Subtracts this Vector with the passed Vector. The Result is a new Vector instance.

Parameters:
vector - Vector to subtract
Returns:
Vector2D

divideWith

public Vector2D divideWith(double value)
Divides this Vector with the value. The Result is a new Vector instance.

Parameters:
value - Divider
Returns:
Vector2D

multipyWith

public Vector2D multipyWith(double value)
Multiplies this Vector with the passed value. The Result is a new Vector instance.

Returns:
Vector2D value to multiply

dotProduct

public double dotProduct(Vector2D vector)
Returns the dot product of this Vector and the passed Vector. The Result is a new Vector instance.

Parameters:
vector -
Returns:
dotProduct

crossProduct

public double crossProduct(Vector2D vector)
Returns the cross product of this Vector and the passed Vector. The Result is a new Vector instance.

Parameters:
vector -
Returns:
crossProduct

normalize

public Vector2D normalize()
Normalizes the vector to maintain the same direction but with a absolute. length of 1 and returns a new one with the resulting vector.

Returns:
Normalized Vector2D

distance2Vector

public double distance2Vector(Vector2D vector)
Returns the distance between this Vector and the passed Vector. The result is always a positive double value.

Parameters:
vector - Vektor2
Returns:
distance

avgDistance2VectorArray

public double avgDistance2VectorArray(Vector2D[] vertexes)
Returns the average distance between this Vector and the passed array of Vectors. The result is always a positive double value.

Parameters:
vertexes -
Returns:
average distance

maxDistance2VectorArray

public double maxDistance2VectorArray(Vector2D[] vectorArray)
Returns the max distance between this Vector and the passed array of Vectors. The result is always a positive double value.

Parameters:
vectorArray -
Returns:
max distance

setLocation

public void setLocation(double x,
                        double y)
Sets the coordinates of the Vector to a new position

Parameters:
x - x-coordinate
y - y-coordinate

clone

public java.lang.Object clone()
Returns a clone of this Vector

See Also:
Object.clone()

equals

public boolean equals(java.lang.Object obj)
Returns true, if the coordinates of the Vector equals with the coordinates of the passed Vector (Object).

Parameters:
obj - Vector to compare
Returns:
true, if equals
See Also:
Object.equals(java.lang.Object)

toString

public java.lang.String toString()
Returns the String interpretation of the Vector

See Also:
Object.toString()

getX

public double getX()
Returns:
Returns the x.

getY

public double getY()
Returns:
Returns the y.

getIntX

public int getIntX()
Returns the int value for x

Returns:
(int)x

getIntY

public int getIntY()
Returns the int value for y

Returns:
(int)y