ch.aplu.util
Class GVector

java.lang.Object
  extended by ch.aplu.util.GVector

public class GVector
extends java.lang.Object

Class representing a two-dimensional vector with double coordinates (x, y). The following situation is assumed:


     (0, 0) o------------------>x-axis
            | .  / direction
            |  .
            |   .vector
            |    .
            |     .
            |      *
            v
          y-axis

 
The direction of the vector is defined as the angle (0..2*pi) of the vector direction clockwise with reference to the x-coordinate direction.


Field Summary
 double x
          The public x-coordinate.
 double y
          The public y-coordinate.
 
Constructor Summary
GVector()
          Constructs a zero vector (with coordinates (0, 0)).
GVector(double x, double y)
          Constructs a vector from given double x-y-coordinates.
GVector(float x, float y)
          Constructs a vector from given float x-y-coordinates.
GVector(int x, int y)
          Constructs a vector from given integer x-y-coordinates.
GVector(java.awt.Point pt)
          Constructs a vector from integer coordinates using jawa.awt.Point.
 
Method Summary
 GVector add(GVector v)
          Returns a new vector that is the vector sum of the current vector and the given vector.
 GVector clone()
          Returns a new vector with the same coordinates as the current vector.
 double distanceTo(GVector v)
          Returns the distance between the current vector and the given vector.
 double dot(GVector v)
          Returns the scalar product (dot product) of the current vector with the given vector.
 double getDirection()
          Returns the direction of the vector (range 0..2*pi)
 GVector getNormalized()
          Returns a new vector with magnitude 1 in the direction of the given vector.
 GVector invert()
          Returns a new vector with inverted coordinates.
 boolean isEqual(GVector v)
          Returns true, if the current vector is identical to the given vector.
 boolean isIntersecting(GVector imageCenter, double imageDirection, java.awt.image.BufferedImage image, boolean isRotatable)
          Returns true, if the current vector (casted to int values) points to a non-transparant pixel of the given image.
 double magnitude()
          Returns the magnitude (length) of the vector.
 double magnitude2()
          Returns the square of the magnitude, without squareroot calculation.
 GVector mult(double b)
          Returns a new vector that is the product by a scalar of the current vector and the given double.
 GVector mult(float b)
          Returns a new vector that is the product by a scalar of the current vector and the given float.
 GVector mult(int b)
          Returns a new vector that is the product by a scalar of the current vector and the given integer.
 void normalize()
          Modifies the vector to unit magnitude.
 void rotate(double angle)
          Rotates the vector by the specified angle.
 GVector sub(GVector v)
          Returns a new vector that is the vector difference of the current vector and the given vector.
 java.lang.String toString()
          Returns a string with the x-y-coordinates in the format (x, y).
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

x

public double x
The public x-coordinate.


y

public double y
The public y-coordinate.

Constructor Detail

GVector

public GVector()
Constructs a zero vector (with coordinates (0, 0)).


GVector

public GVector(java.awt.Point pt)
Constructs a vector from integer coordinates using jawa.awt.Point.

Parameters:
pt - the point that becomes a vector

GVector

public GVector(int x,
               int y)
Constructs a vector from given integer x-y-coordinates.

Parameters:
x - the x-coordinate of the vector
y - the y-coordinate of the vector

GVector

public GVector(float x,
               float y)
Constructs a vector from given float x-y-coordinates.

Parameters:
x - the x-coordinate of the vector
y - the y-coordinate of the vector

GVector

public GVector(double x,
               double y)
Constructs a vector from given double x-y-coordinates.

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

magnitude

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

Returns:
the magnitude

magnitude2

public double magnitude2()
Returns the square of the magnitude, without squareroot calculation. This is useful when performance is an issue.

Returns:
the square of the magnitude

normalize

public void normalize()
Modifies the vector to unit magnitude.


getNormalized

public GVector getNormalized()
Returns a new vector with magnitude 1 in the direction of the given vector.

Returns:
a unit vector with same direction

getDirection

public double getDirection()
Returns the direction of the vector (range 0..2*pi)

Returns:
the direction of the vector (in radian, zero to east, clockwise)

rotate

public void rotate(double angle)
Rotates the vector by the specified angle.

Parameters:
angle - the rotation angle (in radian, clockwise)

dot

public double dot(GVector v)
Returns the scalar product (dot product) of the current vector with the given vector.

Parameters:
v - the vector to take for the dot product
Returns:
the dot product

distanceTo

public double distanceTo(GVector v)
Returns the distance between the current vector and the given vector.

Parameters:
v - the vector to take for the distance measurement
Returns:
the distance (magnitude of the vector difference)

add

public GVector add(GVector v)
Returns a new vector that is the vector sum of the current vector and the given vector. Be aware that the current vector is not modified.

Parameters:
v - the vector to be added to the current vector
Returns:
the sum of the two vectors

invert

public GVector invert()
Returns a new vector with inverted coordinates. Be aware that the current vector is not modified.

Returns:
the inverted vector

sub

public GVector sub(GVector v)
Returns a new vector that is the vector difference of the current vector and the given vector. Be aware that the current vector is not modified.

Parameters:
v - the vector to be substracted from the current vector
Returns:
the difference of the two vectors

mult

public GVector mult(int b)
Returns a new vector that is the product by a scalar of the current vector and the given integer. Be aware that the current vector is not modified.

Parameters:
b - the integer scale factor
Returns:
the vector that is scaled with the given integer

mult

public GVector mult(float b)
Returns a new vector that is the product by a scalar of the current vector and the given float. Be aware that the current vector is not modified.

Parameters:
b - the float scale factor
Returns:
the vector that is scaled with the given float

mult

public GVector mult(double b)
Returns a new vector that is the product by a scalar of the current vector and the given double. Be aware that the current vector is not modified.

Parameters:
b - the double scale factor
Returns:
the vector that is scaled with the given double

clone

public GVector clone()
Returns a new vector with the same coordinates as the current vector.

Overrides:
clone in class java.lang.Object
Returns:
a clone of the current vector

toString

public java.lang.String toString()
Returns a string with the x-y-coordinates in the format (x, y).

Overrides:
toString in class java.lang.Object
Returns:
a string representation of the vector

isIntersecting

public boolean isIntersecting(GVector imageCenter,
                              double imageDirection,
                              java.awt.image.BufferedImage image,
                              boolean isRotatable)
Returns true, if the current vector (casted to int values) points to a non-transparant pixel of the given image.

Parameters:
imageCenter - the vector to the center of the image
imageDirection - the direction of the image (angle of edge ulx --> uly with respect to the positive x-direction (0..2*pi))
isRotatable - if true, the imageDirection is considered; otherwise imageDirection = 0 is assumed
Returns:
true, if the vector points to non-transparent pixels of the image

isEqual

public boolean isEqual(GVector v)
Returns true, if the current vector is identical to the given vector.

Parameters:
v - the vector to compare
Returns:
true, if both vectors are identical