de.cau.cs.kieler.core.math
Class KVector

java.lang.Object
  extended by de.cau.cs.kieler.core.math.KVector
All Implemented Interfaces:
IDataObject, Serializable, Cloneable

public class KVector
extends Object
implements IDataObject, Cloneable

A simple 2D vector class which supports translation, scaling, normalization etc.

See Also:
Serialized Form
Rating proposed yellow
(2011-01-13) msp

Field Summary
static double FULL_CIRCLE
          one full turn in a circle in degrees (360°).
 double x
          x coordinate.
 double y
          y coordinate.
 
Constructor Summary
KVector()
          Create vector with default coordinates (0,0).
KVector(double alpha)
          Creates a normalized vector for the passed angle in degree.
KVector(double thex, double they)
          Constructs a new vector from given values.
KVector(KVector v)
          Creates an exact copy of a given vector v.
 
Method Summary
 KVector add(KVector v)
          Vector addition.
static KVector add(KVector v1, KVector v2)
          Returns the sum of two given vectors as a new vector instance.
 KVector applyBounds(double lowx, double lowy, double highx, double highy)
          Apply the given bounds to this vector.
 KVector clone()
          returns an exact copy of this vector.
 KVector differenceCreate(KVector v)
          Create a difference from this vector and another vector.
 double distance(KVector v2)
          Returns the distance between two vectors.
static double distance(KVector v1, KVector v2)
          Returns the distance between two vectors.
 boolean equals(Object obj)
          
 double getLength()
          returns this vector's length.
 double getSquareLength()
          returns square length of this vector.
 int hashCode()
          
 KVector negate()
          Negate the vector.
 KVector normalize()
          Normalize the vector.
 KVector normalizedCreate()
          Create a normalized version of this vector.
 void parse(String string)
          Parse the given string and set the content of this data object.
 double productDot(KVector v2)
          Returns the dot product of the two given vectors.
static double productDot(KVector v1, KVector v2)
          Returns the dot product of the two given vectors.
 KVector reset()
          Set vector to (0,0).
 KVector scale(double scale)
          Scale the vector.
 KVector scale(double scalex, double scaley)
          Scale the vector with different values for X and Y coordinate.
 KVector scaledCreate(double lambda)
          Create a scaled version of this vector.
 KVector scaleToLength(double length)
          scales this vector to the passed length.
 KVector sub(KVector v)
          Vector subtraction.
static KVector sub(KVector v1, KVector v2)
          Returns the subtraction of the two given vectors as a new vector instance.
 KVector sumCreate(KVector v)
          Create a sum from this vector and another vector.
 double toDegrees()
          Returns degree representation of this vector in degree.
 String toString()
          
 KVector translate(double dx, double dy)
          Translate the vector.
 void wiggle(Random random, double amount)
          Add some "noise" to this vector.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

x

public double x
x coordinate.


y

public double y
y coordinate.


FULL_CIRCLE

public static final double FULL_CIRCLE
one full turn in a circle in degrees (360°).

See Also:
Constant Field Values
Constructor Detail

KVector

public KVector()
Create vector with default coordinates (0,0).


KVector

public KVector(double thex,
               double they)
Constructs a new vector from given values.

Parameters:
thex - x value
they - y value

KVector

public KVector(KVector v)
Creates an exact copy of a given vector v.

Parameters:
v - existing vector

KVector

public KVector(double alpha)
Creates a normalized vector for the passed angle in degree.

Parameters:
alpha - angle in [0, 360)
Method Detail

clone

public KVector clone()
returns an exact copy of this vector.

Overrides:
clone in class Object
Returns:
identical vector

toString

public String toString()

Overrides:
toString in class Object

equals

public boolean equals(Object obj)

Overrides:
equals in class Object

hashCode

public int hashCode()

Overrides:
hashCode in class Object

getLength

public double getLength()
returns this vector's length.

Returns:
Math.sqrt(x*x + y*y)

getSquareLength

public double getSquareLength()
returns square length of this vector.

Returns:
x*x + y*y

reset

public final KVector reset()
Set vector to (0,0).

Returns:
this

add

public final KVector add(KVector v)
Vector addition.

Parameters:
v - vector to add
Returns:
this + v

add

public static KVector add(KVector v1,
                          KVector v2)
Returns the sum of two given vectors as a new vector instance.

Parameters:
v1 - first vector
v2 - second vector
Returns:
new vector first + second

sub

public final KVector sub(KVector v)
Vector subtraction.

Parameters:
v - vector to subtract
Returns:
this

sub

public static KVector sub(KVector v1,
                          KVector v2)
Returns the subtraction of the two given vectors as a new vector instance.

Parameters:
v1 - first vector
v2 - second vector
Returns:
new vector first - second

scale

public final KVector scale(double scale)
Scale the vector.

Parameters:
scale - scaling factor
Returns:
this

scale

public final KVector scale(double scalex,
                           double scaley)
Scale the vector with different values for X and Y coordinate.

Parameters:
scalex - the x scaling factor
scaley - the y scaling factor
Returns:
this

translate

public final KVector translate(double dx,
                               double dy)
Translate the vector.

Parameters:
dx - the x offset
dy - the y offset
Returns:
this

normalize

public KVector normalize()
Normalize the vector.

Returns:
this

scaleToLength

public KVector scaleToLength(double length)
scales this vector to the passed length.

Parameters:
length - length to scale to
Returns:
this

negate

public KVector negate()
Negate the vector.

Returns:
this

toDegrees

public double toDegrees()
Returns degree representation of this vector in degree.

Returns:
value within [0,360)

wiggle

public final void wiggle(Random random,
                         double amount)
Add some "noise" to this vector.

Parameters:
random - the random number generator
amount - the amount of noise to add

scaledCreate

public final KVector scaledCreate(double lambda)
Create a scaled version of this vector.

Parameters:
lambda - scaling factor
Returns:
new vector which is this scaled by lambda

normalizedCreate

public final KVector normalizedCreate()
Create a normalized version of this vector.

Returns:
normalized copy of this

sumCreate

public final KVector sumCreate(KVector v)
Create a sum from this vector and another vector.

Parameters:
v - second addend
Returns:
new vector which is the sum of this and v

differenceCreate

public final KVector differenceCreate(KVector v)
Create a difference from this vector and another vector.

Parameters:
v - subtrahend
Returns:
new vector which is the difference between this and v

distance

public double distance(KVector v2)
Returns the distance between two vectors.

Parameters:
v2 - second vector
Returns:
distance between this and second vector

distance

public static double distance(KVector v1,
                              KVector v2)
Returns the distance between two vectors.

Parameters:
v1 - first vector
v2 - second vector
Returns:
distance between first and second

productDot

public double productDot(KVector v2)
Returns the dot product of the two given vectors.

Parameters:
v2 - second vector
Returns:
(this.x * this.x) + (v1.y * v2.y)

productDot

public static double productDot(KVector v1,
                                KVector v2)
Returns the dot product of the two given vectors.

Parameters:
v1 - first vector
v2 - second vector
Returns:
(this.x * this.x) + (v1.y * v2.y)

applyBounds

public KVector applyBounds(double lowx,
                           double lowy,
                           double highx,
                           double highy)
Apply the given bounds to this vector.

Parameters:
lowx - the lower bound for x coordinate
lowy - the lower bound for y coordinate
highx - the upper bound for x coordinate
highy - the upper bound for y coordinate
Returns:
this

parse

public void parse(String string)
Parse the given string and set the content of this data object.

Specified by:
parse in interface IDataObject
Parameters:
string - a string