y25.graphics
Class Vector3D

java.lang.Object
  extended byy25.graphics.Vector3D

public class Vector3D
extends Object

Represents a vector in three dimensional space. A vector's x,y and z coordinates can be retrieved and set by accessing it's x,y and z members directly, e.g. vector.x = 3*vector.z.

Most operations on a Vector3D can be chained, since a reference to the vector is returned by most methods, e.g. vector.set(2,3,4).add(-1,2,3).scale(0.5).reverse().


Field Summary
 float x
          The x-coordinate of the vector.
 float y
          The y-coordinate of the vector.
 float z
          The z-coordinate of the vector.
 
Constructor Summary
Vector3D()
          Creates a null-vector (a vector whose entries are all 0).
Vector3D(float xx, float yy, float zz)
          Creates a vector that points in the given direction.
Vector3D(Point3D p)
          Creates a vector by interpreting the passed point as a vector and copying its coordinates.
Vector3D(Point3D from, Point3D to)
          Creates a vector that points from point from to point to.
Vector3D(Vector3D v)
          Creates a vector that points in the same direction as the passed vector.
 
Method Summary
 Vector3D add(Vector3D v)
          Adds the given vector to this one.
 Vector3D cross(Vector3D v)
          Computes the cross product of this vector with the passed one and stores the result in this vector.
 float dot(Vector3D v)
          Computes the dot product of this vector and the passed one.
 Vector3D getAdded(Vector3D a)
          Adds the given vector to a copy of this one and returns the copy.
 Vector3D getCrossed(Vector3D v)
          Computes the cross product of this vector with the passed one and returns the result.
 int getMaxAxis()
          Determines the most dominant axis of this vector and returns its index.
 int getMinAxis()
          Determines the least dominant axis of this vector and returns its index.
 Vector3D getNormalized()
          Normalizes a copy of this vector and returns the copy.
 Vector3D getOrthogonal()
          Returns a vector that is orthogonal to this one.
 Point3D getPoint3D()
          Returns a Point3D with the same x,y,z entries as this vector.
 Vector3D getReversed()
          Reverses a copy of this vector and returns the copy.
 Vector3D getScaled(float s)
          Scales a copy of this vector by the given scaling factor and returns the copy.
 Vector3D getSubtracted(Vector3D a)
          Subtracts the given vector from a copy of this one and returns the copy.
 boolean isZero()
          Returns true if this is a null-vector (all entries are 0), false otherwise.
 float length()
          Returns the length of this vector.
 Vector3D normalize()
          Normalizes this vector.
 Vector3D reverse()
          Reverses the direction of this vector.
 Vector3D scale(float s)
          Scales this vector by the given scaling factor.
 Vector3D set(float xx, float yy, float zz)
          Sets this vector to point in the given direction.
 Vector3D set(Point3D p)
          Interprets the passed point as a vector and sets this vector accordingly..
 Vector3D set(Point3D from, Point3D to)
          Sets this vector to point from from to to.
 Vector3D set(Vector3D v)
          Sets this vector to point in the same direction as the passed vector.
 Vector3D subtract(Vector3D v)
          Subtracts the given vector from this one.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

x

public float x
The x-coordinate of the vector.


y

public float y
The y-coordinate of the vector.


z

public float z
The z-coordinate of the vector.

Constructor Detail

Vector3D

public Vector3D()
Creates a null-vector (a vector whose entries are all 0).


Vector3D

public Vector3D(Vector3D v)
Creates a vector that points in the same direction as the passed vector.


Vector3D

public Vector3D(float xx,
                float yy,
                float zz)
Creates a vector that points in the given direction.


Vector3D

public Vector3D(Point3D p)
Creates a vector by interpreting the passed point as a vector and copying its coordinates.


Vector3D

public Vector3D(Point3D from,
                Point3D to)
Creates a vector that points from point from to point to.

Method Detail

set

public Vector3D set(float xx,
                    float yy,
                    float zz)
Sets this vector to point in the given direction. Returns a reference to this vector.


set

public Vector3D set(Vector3D v)
Sets this vector to point in the same direction as the passed vector. Returns a reference to this vector.


set

public Vector3D set(Point3D p)
Interprets the passed point as a vector and sets this vector accordingly.. Same as set(new Point3D(0,0,0), p). Returns a reference to this vector.


set

public Vector3D set(Point3D from,
                    Point3D to)
Sets this vector to point from from to to. Returns a reference to this vector.


isZero

public boolean isZero()
Returns true if this is a null-vector (all entries are 0), false otherwise.


length

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


normalize

public Vector3D normalize()
Normalizes this vector. After normalization, this vector's length() equals 1. Returns a reference to this vector.


getNormalized

public Vector3D getNormalized()
Normalizes a copy of this vector and returns the copy.


cross

public Vector3D cross(Vector3D v)
Computes the cross product of this vector with the passed one and stores the result in this vector. Then returns a reference to this vector.

In a right handed coordinate system, where X, Y and Z denote the vectors of the axes, we have: Z = X.cross(Y) and -Z - Y.cross(X).


getCrossed

public Vector3D getCrossed(Vector3D v)
Computes the cross product of this vector with the passed one and returns the result.


reverse

public Vector3D reverse()
Reverses the direction of this vector. Returns a reference to this vector.


getReversed

public Vector3D getReversed()
Reverses a copy of this vector and returns the copy.


scale

public Vector3D scale(float s)
Scales this vector by the given scaling factor. Returns a reference to this vector.


getScaled

public Vector3D getScaled(float s)
Scales a copy of this vector by the given scaling factor and returns the copy.


add

public Vector3D add(Vector3D v)
Adds the given vector to this one. Returns a reference to this vector.


getAdded

public Vector3D getAdded(Vector3D a)
Adds the given vector to a copy of this one and returns the copy.


subtract

public Vector3D subtract(Vector3D v)
Subtracts the given vector from this one. Returns a reference to this vector.


getSubtracted

public Vector3D getSubtracted(Vector3D a)
Subtracts the given vector from a copy of this one and returns the copy.


dot

public float dot(Vector3D v)
Computes the dot product of this vector and the passed one.


toString

public String toString()

getMinAxis

public int getMinAxis()
Determines the least dominant axis of this vector and returns its index.

The least dominant axis of a vector is the one with the smallest absolute value, e.g. for a vector (2,-3,1), the least dominant axis is the z-axis.

The x-axis has index 0, the y-axis has index 1 and the z-axis has index 2.


getMaxAxis

public int getMaxAxis()
Determines the most dominant axis of this vector and returns its index.

The most dominant axis of a vector is the one with the highest absolute value, e.g. for a vector (2,-3,1), the most dominant axis is the y-axis.

The x-axis has index 0, the y-axis has index 1 and the z-axis has index 2.


getOrthogonal

public Vector3D getOrthogonal()
Returns a vector that is orthogonal to this one.

Note that there are infinitely many such vectors (and they all lie on a plane), so this method just returns an arbitrary one of them.


getPoint3D

public Point3D getPoint3D()
Returns a Point3D with the same x,y,z entries as this vector.