unlekker.geom
Class Vec2

java.lang.Object
  extended by unlekker.geom.Vec2

public class Vec2
extends java.lang.Object

2D vector class.

Author:
Marius Watz

Field Summary
static float DEG
           
static float RADIAN
           
 float x
          Position
 float y
          Position
 
Constructor Summary
Vec2()
          Constructs a vector with x=0, y=0.
Vec2(float _x, float _y)
          Constructs a vector with x=_x, y=_y.
Vec2(Vec2 v)
          Constructs a vector with x=v.x, y=v.y.
 
Method Summary
 void add(float vx, float vy)
          Adds vx,vy to this vector. x = x + vx, y = y + vy
 void add(Vec2 v)
          Adds vector v to this vector. x = x + v.x, y = y + v.y
 float angle()
          Returns the orientation (angle) of the current vector in radians.
static float angle(float x, float y)
          Returns the orientation (angle) of the vector in radians.
 void div(float d)
          Divides the values of current vector by the float d.
static float dot(Vec2 v1, Vec2 v2)
          Calculates dot product of two vectors v1 and v2.
 float findDistance(float _x, float _y)
          Returns distance between the point given by x,y and the point given by _x, _y.
 float findDistance(Vec2 v)
          Returns distance between the point given by x,y and the point given by v.x, v.y.
static Vec2 findIntersection(Vec2 p1, Vec2 p2, Vec2 p3, Vec2 p4)
          Ccalculates intersection and checks for parallel lines.
 float length()
          Calculates length of vector.
static float length(float x, float y)
          Calculates length of vector.
 void mult(float m)
          Scales current vector by single scaling factor m.
 void mult(float vx, float vy)
          Multiplies values of vx,vy with values of vector v.
 void mult(Vec2 v)
          Multiplies values of current vector with values of vector v.
 void norm()
          Normalizes the current vector to be 1 unit in length.
 void norm(float m)
          Normalizes the current vector to be m units in length.
 void rotate(float radians)
          Rotates vector by a degree given in radians
static Vec2 rotate(Vec2 v1, float deg)
          Rotates vector by a degree given in radians.
 void set(float tx, float ty)
          Sets vector to x = tx, y=ty.
 void set(Vec2 v)
          Sets vector to the values of v.
 void sub(float vx, float vy)
          Subtracts vx,vy from this vector. x = x - vx, y = y - vy
 void sub(Vec2 v)
          Subtracts vector v from this vector. x = x - v.x, y = y - v.y
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEG

public static final float DEG
See Also:
Constant Field Values

RADIAN

public static final float RADIAN
See Also:
Constant Field Values

x

public float x
Position


y

public float y
Position

Constructor Detail

Vec2

public Vec2()
Constructs a vector with x=0, y=0.


Vec2

public Vec2(float _x,
            float _y)
Constructs a vector with x=_x, y=_y.


Vec2

public Vec2(Vec2 v)
Constructs a vector with x=v.x, y=v.y.

Method Detail

set

public void set(Vec2 v)
Sets vector to the values of v.

Parameters:
v -

set

public void set(float tx,
                float ty)
Sets vector to x = tx, y=ty.

Parameters:
tx, - ty

toString

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

add

public void add(Vec2 v)
Adds vector v to this vector. x = x + v.x, y = y + v.y

Parameters:
v -

add

public void add(float vx,
                float vy)
Adds vx,vy to this vector. x = x + vx, y = y + vy

Parameters:
vx, - vy

sub

public void sub(Vec2 v)
Subtracts vector v from this vector. x = x - v.x, y = y - v.y

Parameters:
v -

sub

public void sub(float vx,
                float vy)
Subtracts vx,vy from this vector. x = x - vx, y = y - vy

Parameters:
vx, - vy

mult

public void mult(Vec2 v)
Multiplies values of current vector with values of vector v. x = x * v.x, y = y*v.y

Parameters:
v -

mult

public void mult(float vx,
                 float vy)
Multiplies values of vx,vy with values of vector v. x = x * vx, y = y*vy

Parameters:
vx,vy -

mult

public void mult(float m)
Scales current vector by single scaling factor m. x = x * m, y = y* m

Parameters:
m -

div

public void div(float d)
Divides the values of current vector by the float d. x = x / d, y = y / d

Parameters:
m -

norm

public void norm()
Normalizes the current vector to be 1 unit in length.


norm

public void norm(float m)
Normalizes the current vector to be m units in length.


findDistance

public float findDistance(Vec2 v)
Returns distance between the point given by x,y and the point given by v.x, v.y.

Parameters:
v - Point to compare with
Returns:
distance

findDistance

public float findDistance(float _x,
                          float _y)
Returns distance between the point given by x,y and the point given by _x, _y.

Parameters:
_x - X position to compare with
_y - Y position to compare with
Returns:
distance

angle

public static float angle(float x,
                          float y)
Returns the orientation (angle) of the vector in radians. This is a static version, which can be called without instantiating the class: float angle=Vec2.angle(50,100);

Parameters:
x -
y -
Returns:
The orientation (angle) of the vector in radians.

angle

public float angle()
Returns the orientation (angle) of the current vector in radians.

Returns:
The orientation (angle) of the current vector in radians.

findIntersection

public static Vec2 findIntersection(Vec2 p1,
                                    Vec2 p2,
                                    Vec2 p3,
                                    Vec2 p4)
Ccalculates intersection and checks for parallel lines. Also checks that the intersection point is actually on the line segment p1-p2

Parameters:
p1 - First point of first line
p2 - Second point of first line
p3 - First point of first line
p4 - Second point of first line
Returns:
Point of intersection if found, null if not found.

dot

public static float dot(Vec2 v1,
                        Vec2 v2)
Calculates dot product of two vectors v1 and v2.

Returns:
dot product

length

public float length()
Calculates length of vector.

Returns:
length in units

length

public static float length(float x,
                           float y)
Calculates length of vector. Static version, can be called without instantiating an object.

Returns:
length in units

rotate

public void rotate(float radians)
Rotates vector by a degree given in radians

Parameters:
radians - Degree to rotate by, in radians.

rotate

public static Vec2 rotate(Vec2 v1,
                          float deg)
Rotates vector by a degree given in radians. Static version.

Parameters:
radians - Degree to rotate by, in radians.