fi.utu.cs.physics
Class Vector2

java.lang.Object
  |
  +--fi.utu.cs.physics.Tuple2
        |
        +--fi.utu.cs.physics.Vector2
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable

public class Vector2
extends Tuple2
implements java.lang.Comparable

A two-component vector of double-precision floating point values.


Field Summary
static Vector2 ZERO
          A vector with the length 0.0.
 
Constructor Summary
Vector2(double dx, double dy)
          Creates a new two-component vector with the given values.
Vector2(double x1, double y1, double x2, double y2)
          Creates a new two-component vector from the given points.
Vector2(Point2 source, Point2 target)
          Creates a new two-component vector from the given points.
 
Method Summary
 void add(double dx, double dy)
          Adds values to the components.
 void add(Vector2 other)
          Adds the components of other to the components.
 double angle(Vector2 other)
          Returns the angle between this vector and the vector other.
 int compareTo(java.lang.Object o)
          Returns a negative integer, zero, or a positive integer as the length of this vector is less than, equal to, or greater than the length of vector o.
 double getDeltaX()
          Returns the horizontal component.
 double getDeltaY()
          Returns the vertical component.
 boolean isInTheSameQuadrant(Vector2 other)
          Returns whether the vector other resides in the same coordinate quadrant as this vector.
 double length()
          Returns the length of this vector.
 double lengthSquared()
          Returns the squared length of this vector.
 void multiply(double s)
          Multiplies the vector with the scalar s.
 Vector2 project(Vector2 other)
          Returns a projection of the vector other to this vector.
 void rotate(double r)
          Rotates the vector counterclockwise.
 void rotate180()
          Rotates the vector 180 degrees counterclockwise.
 void rotate270()
          Rotates the vector 270 degrees counterclockwise.
 void rotate90()
          Rotates the vector 90 degrees counterclockwise.
 double scalarProduct(Vector2 other)
          Returns the scalar product of this vector and the vector other.
 void set(double dx, double dy)
          Sets the horizontal and vertical component.
 void set(Point2 source, Point2 target)
          Sets the horizontal and vertical components to correspond the transfer from the point source to the point target.
 void set(Vector2 v)
          Sets the horizontal and vertical components to the same values as in other.
 void setDeltaX(double dx)
          Sets the horizontal component to the value dx.
 void setDeltaY(double dy)
          Sets the vertical component to the value dy.
 Point2 transfer(Point2 source)
          Creates and returns a point that is transfered from the point source according to this vector.
 Vector2 unitVector()
          Returns a unit vector corresponding to this vector.
 
Methods inherited from class fi.utu.cs.physics.Tuple2
add, clone, equals, firstElementSign, getFirstElement, getSecondElement, hashCode, max, min, negate, negateFirstElement, negateSecondElement, secondElementSign, set, setFirstElement, setSecondElement, swap, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ZERO

public static final Vector2 ZERO
A vector with the length 0.0.

Constructor Detail

Vector2

public Vector2(double dx,
               double dy)
Creates a new two-component vector with the given values.


Vector2

public Vector2(double x1,
               double y1,
               double x2,
               double y2)
Creates a new two-component vector from the given points. In the new vector, this.getDeltaX() == x2 - x1 and this.getDeltaY() == y2 - y1.


Vector2

public Vector2(Point2 source,
               Point2 target)
Creates a new two-component vector from the given points. In the new vector, this.getDeltaX() == target.getX() - source.getX() and this.getDeltaY() == target.getY() - source.getY().

Method Detail

getDeltaX

public double getDeltaX()
Returns the horizontal component.


getDeltaY

public double getDeltaY()
Returns the vertical component.


setDeltaX

public void setDeltaX(double dx)
Sets the horizontal component to the value dx.


setDeltaY

public void setDeltaY(double dy)
Sets the vertical component to the value dy.


set

public void set(double dx,
                double dy)
Sets the horizontal and vertical component.

Overrides:
set in class Tuple2
Parameters:
dx - The value of the horizontal component.
dy - The value of the vertical component.
See Also:
set(Point2,Point2), set(Vector2)

set

public void set(Point2 source,
                Point2 target)
Sets the horizontal and vertical components to correspond the transfer from the point source to the point target. After the operation, this.getDeltaX() = target.getX() - this.getX() and this.getDeltaY() = target.getY() - this.getY().

See Also:
set(Vector2)

set

public void set(Vector2 v)
Sets the horizontal and vertical components to the same values as in other.


length

public double length()
Returns the length of this vector.


lengthSquared

public double lengthSquared()
Returns the squared length of this vector.


add

public void add(double dx,
                double dy)
Adds values to the components.

Overrides:
add in class Tuple2
Parameters:
dx - The value added to the horizontal component.
dy - The value added to the vertical component.
See Also:
add(Vector2)

add

public void add(Vector2 other)
Adds the components of other to the components.


multiply

public void multiply(double s)
Multiplies the vector with the scalar s.

Overrides:
multiply in class Tuple2
Since:
1.1

scalarProduct

public double scalarProduct(Vector2 other)
Returns the scalar product of this vector and the vector other.

Since:
1.1

unitVector

public Vector2 unitVector()
Returns a unit vector corresponding to this vector. More formally, this.unitVector().length == 1.0 and this.angle(this.unitVector()) == 0.0.

Since:
1.1

project

public Vector2 project(Vector2 other)
Returns a projection of the vector other to this vector.

Since:
1.1

angle

public double angle(Vector2 other)
Returns the angle between this vector and the vector other.

Returns:
angle in radians from the range [0.0, Math.PI]
Since:
1.1
See Also:
rotate(double)

transfer

public Point2 transfer(Point2 source)
Creates and returns a point that is transfered from the point source according to this vector. In the new point, getX() = source.getX() + this.getDeltaX() and getY() = source.getY() + this.getDeltaY().


rotate90

public void rotate90()
Rotates the vector 90 degrees counterclockwise.

See Also:
rotate(double), rotate180(), rotate270()

rotate180

public void rotate180()
Rotates the vector 180 degrees counterclockwise.

See Also:
rotate(double), rotate90(), rotate270()

rotate270

public void rotate270()
Rotates the vector 270 degrees counterclockwise.

See Also:
rotate(double), rotate90(), rotate180()

rotate

public void rotate(double r)
Rotates the vector counterclockwise.

Parameters:
r - The amount of rotation in radians.
See Also:
angle(Vector2), rotate90(), rotate180(), rotate270()

isInTheSameQuadrant

public boolean isInTheSameQuadrant(Vector2 other)
Returns whether the vector other resides in the same coordinate quadrant as this vector.

Since:
1.1

compareTo

public int compareTo(java.lang.Object o)
Returns a negative integer, zero, or a positive integer as the length of this vector is less than, equal to, or greater than the length of vector o.

Specified by:
compareTo in interface java.lang.Comparable
Throws:
java.lang.ClassCastException - If the argument is not of the type Vector2.