vooga.physicsEngine.geometry.mathUtils
Class Vector2f

java.lang.Object
  extended by vooga.physicsEngine.geometry.mathUtils.Vector2f

public class Vector2f
extends java.lang.Object

A utility class representing a 2-dimensional vector. Some of this code is copied from an online open-source implementation by Kevin Glass.

Author:
Peng Shi, Sami Boghos

Field Summary
 double delta
           
protected  double x
           
protected  double y
           
 
Constructor Summary
Vector2f()
          Create a (0,0) vector.
Vector2f(double x, double y)
          Create a new vector
Vector2f(Vector2f other)
          Create a new vector based on another
 
Method Summary
 Vector2f add(Vector2f difference)
           
 Vector2f clone()
           
 double cross(Vector2f vec)
          Computes the magnitude of the cross product with given vector.
static double cross(Vector2f a, Vector2f b)
          Return the magnitude of the cross product bewteen a and b (a.x*b.y-a.y*b.x)
static Vector2f difference(Vector2f first, Vector2f second)
          Compute the vector difference first-second
 double distance(Vector2f other)
          Get the distance from this point to another
 double distanceSquared(Vector2f other)
          Get the distance squared from this point to another
 double dot(Vector2f vec)
          Compute the dot product between a and b
static double dot(Vector2f a, Vector2f b)
          Returns the dot product of a and b.
 boolean equals(java.lang.Object other)
           
 boolean equalsDelta(Vector2f other, double delta)
          Compare two vectors allowing for a (small) error as indicated by the delta.
static Vector2f fromPosition(Position position)
           
static Vector2f getRealCordinates(Vector2f relativeCoordinate, Vector2f centroid, double angularPosition)
          Obtain the real coordinate of some point given that the old coordinate has centroid is (0,0) and is unrotated.`
 double getX()
          Get the x position of the vector
 double getY()
          Get the y position of the vector
 double length()
          Computes the Euclidean length of this vector.
 double lengthSquared()
          The length of the vector squared
static Vector2f mean(java.util.List<Vector2f> points)
          Return the mean of the given vectors; returning (0,0) if the mean is empty
static Vector2f mult(Vector2f vector, double scalar)
          Return the scalar product of the vector and scalar
 Vector2f normalise()
          Normalise the vector
static Vector2f normalize(Vector2f vec)
          Normalize the given vector without changing it.
 Vector2f reverse()
          Reverse this vector (no copying)
static Vector2f rotateBy90CCW(Vector2f vec)
          Return a new vector that is the given rotated by 90 degrees counterclockwise
static Vector2f rotateBy90CW(Vector2f vec)
          Return a new vector that is the given rotated by 90 degrees clockwise
 Vector2f scale(double a)
          Scale this vector by a value
 void set(double x, double y)
          Change values of a vector
 void set(float x, float y)
          Set the values in this vector
 void set(Vector2f other)
          Set the value of this vector
 Vector2f sub(Vector2f v)
          Subtract a vector from this vector
static Vector2f sum(java.util.List<Vector2f> summands)
          Sum a list of vectors and return the result
static Vector2f sum(Vector2f a, Vector2f b)
          Find the sum of vectors a and b
static Position toPosition(Vector2f vector)
          Translates given Vector2f into a game engine Position object.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

x

protected double x

y

protected double y

delta

public final double delta
See Also:
Constant Field Values
Constructor Detail

Vector2f

public Vector2f(double x,
                double y)
Create a new vector

Parameters:
x - The x component
y - The y component

Vector2f

public Vector2f(Vector2f other)
Create a new vector based on another

Parameters:
other - The other vector to copy into this one

Vector2f

public Vector2f()
Create a (0,0) vector.

Method Detail

clone

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

getX

public double getX()
Get the x position of the vector

Returns:
The x position of the vector

getY

public double getY()
Get the y position of the vector

Returns:
The y position of the vector

set

public void set(double x,
                double y)
Change values of a vector

Parameters:
x - The new x value of the vector
y - The new y value of the vector

add

public Vector2f add(Vector2f difference)
Parameters:
difference -

fromPosition

public static Vector2f fromPosition(Position position)

set

public void set(Vector2f other)
Set the value of this vector

Parameters:
other - The values to set into the vector

dot

public double dot(Vector2f vec)
Compute the dot product between a and b

Parameters:
vec -
Returns:
dot product

set

public void set(float x,
                float y)
Set the values in this vector

Parameters:
x - The x component to set
y - The y component to set

reverse

public Vector2f reverse()
Reverse this vector (no copying)

Returns:
this vector negated

sub

public Vector2f sub(Vector2f v)
Subtract a vector from this vector

Parameters:
v - The vector subtract

scale

public Vector2f scale(double a)
Scale this vector by a value

Parameters:
a - The value to scale this vector by

normalise

public Vector2f normalise()
Normalise the vector


lengthSquared

public double lengthSquared()
The length of the vector squared

Returns:
The length of the vector squared

length

public double length()
Computes the Euclidean length of this vector.

Returns:

toString

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

distance

public double distance(Vector2f other)
Get the distance from this point to another

Parameters:
other - The other point we're measuring to
Returns:
The distance to the other point

distanceSquared

public double distanceSquared(Vector2f other)
Get the distance squared from this point to another

Parameters:
other - The other point we're measuring to
Returns:
The distance to the other point

equalsDelta

public boolean equalsDelta(Vector2f other,
                           double delta)
Compare two vectors allowing for a (small) error as indicated by the delta. Note that the delta is used for the vector's components separately, i.e. any other vector that is contained in the square box with sides 2*delta and this vector at the center is considered equal.

Parameters:
other - The other vector to compare this one to
delta - The allowed error
Returns:
True iff this vector is equal to other, with a tolerance defined by delta

equals

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

sum

public static Vector2f sum(java.util.List<Vector2f> summands)
Sum a list of vectors and return the result

Parameters:
summands -
Returns:
the vector sum

sum

public static Vector2f sum(Vector2f a,
                           Vector2f b)
Find the sum of vectors a and b

Parameters:
a -
b -
Returns:
new vector representing their sum

mean

public static Vector2f mean(java.util.List<Vector2f> points)
Return the mean of the given vectors; returning (0,0) if the mean is empty

Parameters:
points -
Returns:

cross

public static double cross(Vector2f a,
                           Vector2f b)
Return the magnitude of the cross product bewteen a and b (a.x*b.y-a.y*b.x)

Parameters:
vec -
Returns:

cross

public double cross(Vector2f vec)
Computes the magnitude of the cross product with given vector.

Parameters:
vec -
Returns:

getRealCordinates

public static Vector2f getRealCordinates(Vector2f relativeCoordinate,
                                         Vector2f centroid,
                                         double angularPosition)
Obtain the real coordinate of some point given that the old coordinate has centroid is (0,0) and is unrotated.`

Parameters:
relativeCoordinate -
centroid -
angularPosition -
Returns:

difference

public static Vector2f difference(Vector2f first,
                                  Vector2f second)
Compute the vector difference first-second

Parameters:
first -
second -
Returns:

rotateBy90CCW

public static Vector2f rotateBy90CCW(Vector2f vec)
Return a new vector that is the given rotated by 90 degrees counterclockwise

Parameters:
vec -
Returns:
new rotated vector

rotateBy90CW

public static Vector2f rotateBy90CW(Vector2f vec)
Return a new vector that is the given rotated by 90 degrees clockwise

Parameters:
vec -
Returns:
new rotated vector

dot

public static double dot(Vector2f a,
                         Vector2f b)
Returns the dot product of a and b.

Parameters:
a -
b -
Returns:

mult

public static Vector2f mult(Vector2f vector,
                            double scalar)
Return the scalar product of the vector and scalar

Parameters:
vector -
scalar -
Returns:
vector scaled by scalar

normalize

public static Vector2f normalize(Vector2f vec)
Normalize the given vector without changing it.

Parameters:
vec -
Returns:

toPosition

public static Position toPosition(Vector2f vector)
Translates given Vector2f into a game engine Position object.

Parameters:
vector -
Returns: