ch.aplu.jgamegrid
Class GGVector

java.lang.Object
  extended by ch.aplu.jgamegrid.GGVector

public class GGVector
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
GGVector()
          Constructs a zero vector (with coordinates (0, 0)).
GGVector(double x, double y)
          Constructs a vector from given double x-y-coordinates.
GGVector(float x, float y)
          Constructs a vector from given float x-y-coordinates.
GGVector(int x, int y)
          Constructs a vector from given integer x-y-coordinates.
GGVector(java.awt.Point pt)
          Constructs a vector from integer coordinates using jawa.awt.Point.
 
Method Summary
 GGVector add(GGVector v)
          Returns a new vector that is the vector sum of the current vector and the given vector.
 GGVector clone()
          Returns a new vector with the same coordinates as the current vector.
 double distanceTo(GGVector v)
          Returns the distance between the current vector and the given vector.
 double dot(GGVector 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)
 GGVector getNormalized()
          Returns a new vector with magnitude 1 in the direction of the given vector.
 GGVector invert()
          Returns a new vector with inverted coordinates.
 boolean isEqual(GGVector v)
          Returns true, if the current vector is identical to the given vector.
 boolean isIntersecting(GGVector 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.
 GGVector mult(double b)
          Returns a new vector that is the product by a scalar of the current vector and the given double.
 GGVector mult(float b)
          Returns a new vector that is the product by a scalar of the current vector and the given float.
 GGVector 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.
 GGVector sub(GGVector 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

GGVector

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


GGVector

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

Parameters:
pt - the point that becomes a vector

GGVector

public GGVector(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

GGVector

public GGVector(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

GGVector

public GGVector(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 GGVector 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(GGVector 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(GGVector 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 GGVector add(GGVector 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 GGVector invert()
Returns a new vector with inverted coordinates. Be aware that the current vector is not modified.

Returns:
the inverted vector

sub

public GGVector sub(GGVector 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 GGVector 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 GGVector 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 GGVector 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 GGVector 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(GGVector 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(GGVector 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