common
Class Vector

Object
  extended by common:Vector

class 
Object:Vector

Vector class.

The Vector class allows you to easily define a four-dimensional vector. These have a variety of uses in fractal generation, including use as a container for a 3D vector.

For performance reasons, none of the Vector methods return a vector. The last vector argument is the return value. For example,

Vector V1 = new Vector(....)
V1.Cross(V2,V3)

is equivalent to

V3 = V1 x V2

Also for performance reasons, you may access the vector elements directly, as m_x, m_y, m_z, and m_w.

Ron Barnett


Ultra Fractal Source

Toggle UF Source Code Display

 class Vector {
   ; Vector class.
   ;
   ; <p>
   ; The Vector class allows you to easily define a four-dimensional vector.
   ; These have a variety of uses in fractal generation, including use as a
   ; container for a 3D vector.
   ; <p>
   ; For performance reasons, none of the Vector methods return a vector.
   ; The last vector argument is the return value. For example,
   ; <p>
   ;   Vector V1 = new Vector(....)<br>
   ;   V1.Cross(V2,V3)
   ; <p>
   ; is equivalent to
   ; <p>
   ;   V3 = V1 x V2
   ; <p>
   ; Also for performance reasons, you may access the vector elements
   ; directly, as m_x, m_y, m_z, and m_w.
   ; <p>
   ; Ron Barnett
 
 public:
 
   ; Constructor
   ;
   ; @param x x term of vector
   ; @param y y term of vector
   ; @param z z term of vector
   ; @param w w term of vector (use 1.0 if you are storing a 3D vector)
   func Vector(float x, float y, float z, float w)
     Init(x,y,z,w)
   endfunc
 
   ; Re-initialize a Vector.
   ; <p>
   ; If you need to completely change the value of a Vector, you can use this function rather than deleting the old Vector and creating a new one. (Re-use is faster.)
   ;
   ; @param x x term of vector
   ; @param y y term of vector
   ; @param z z term of vector
   ; @param w w term of vector (use 1.0 if you are storing a 3D vector)
   func Init(float x, float y, float z, float w)
     m_x = x
     m_y = y
     m_z = z
     m_w = w
   endfunc
 
   ; Copy a Vector to another Vector.
   ; <p>
   ; If you simply assign the value of one Vector to another, you actually create a reference in both variables to the same Vector object, and changes in one variable's vector will appear in the other. See the UF help file topic on "reference" ("Objects") for details. To make a true copy from one Vector to another Vector, use this function.
   ;
   ; @param T target vector that will receive a copy of the Vector's data
   func Copy(Vector T)
     T.m_x = m_x
     T.m_y = m_y
     T.m_z = m_z
     T.m_w = m_w
   endfunc
 
   ; Get a Vector element by index.
   ; <p>
   ; Sometimes you need to access a Vector's elements by index number (0-3) rather than by name. If you do, you may use this function. Note that this is slower than accessing the member variables directly.
   ;
   ; @param n element number (0-3) to retrieve
   ; @return the value of the element
   float func Get(int n)
     if (n == 0)
       return m_x
     elseif (n == 1)
       return m_y
     elseif (n == 2)
       return m_z
     elseif (n == 3)
       return m_w
     else
       return 0
     endif
   endfunc
 
   ; Set a Vector element by index.
   ; <p>
   ; Sometimes you need to set a Vector's elements by index number (0-3) rather than by name. If you do, you may use this function. Note that this is slower than accessing the member variables directly.
   ;
   ; @param n element number (0-3) to set
   ; @param v the new value for the element
   func Set(int n, float v)
     if (n == 0)
       m_x = v
     elseif (n == 1)
       m_y = v
     elseif (n == 2)
       m_z = v
     elseif (n == 3)
       m_w = v
     endif
   endfunc
 
   ; Normalize a Vector.
   ; <p>
   ; Normalization gives a unit vector. The x, y, z and w values are
   ; direction cosines.
   ;
   ; @param V target vector that will receive the normalized vector
   func Normalize(Vector V)
     float vd = 1/sqrt(m_x^2+m_y^2+m_z^2+m_w^2)
     V.m_x = m_x*vd
     V.m_y = m_y*vd
     V.m_z = m_z*vd
     V.m_w = m_w*vd
   endfunc
 
   ; Determine a Vector's size.
   ;
   ; @return the size (or length) of the vector
   float func Size()
     return sqrt(m_x^2+m_y^2+m_z^2+m_w^2)
   endfunc
 
   ; Add two Vectors.
   ;
   ; @param V1 the second vector to add
   ; @param V2 the target vector for the added vectors to be stored in
   func Add(Vector V1, Vector V2)
     V2.m_x = m_x+V1.m_x
     V2.m_y = m_y+V1.m_y
     V2.m_z = m_z+V1.m_z
     V2.m_w = m_w+V1.m_w
   endfunc
 
   ; Subtract two Vectors.
   ;
   ; @param V1 the second vector to subtract
   ; @param V2 the target vector for the differenced vectors to be stored in
   func Sub(Vector V1, Vector V2)
     V2.m_x = m_x-V1.m_x
     V2.m_y = m_y-V1.m_y
     V2.m_z = m_z-V1.m_z
     V2.m_w = m_w-V1.m_w
   endfunc
 
   ; Multiply a vector by a constant.
   ;
   ; @param c the constant to multiply by
   ; @param V the target vector for the multiplied values to be stored in
   func MConst(float c, Vector V)
     V.m_x = m_x*c
     V.m_y = m_y*c
     V.m_z = m_z*c
     V.m_w = m_w*c
   endfunc
 
   ; Shift Vector elements.
   ; <p>
   ; This function shifts the vector elements so that x becomes y, y becomes z, z becomes w, and w becomes x. You may shift by any number of positions, although any multiple of four will return the elements to their starting positions. Negative values will be interpreted as shifts in reverse.
   ;
   ; @param shift number of places to shift (negative values shift in reverse)
   ; @param V the target vector to store the shifted vector in
   func Shift(int shift, Vector V)
     float x = m_x
     float y = m_y
     float z = m_z
     float w = m_w
     shift = shift % 4
     if shift < 0
       shift = shift + 4
     endif
     if shift == 1
       V.m_x = w
       V.m_y = x
       V.m_z = y
       V.m_w = z
     elseif shift == 2
       V.m_x = z
       V.m_y = w
       V.m_z = x
       V.m_w = y
     elseif shift == 3
       V.m_x = y
       V.m_y = z
       V.m_z = w
       V.m_w = x
     else
       V.m_x = x
       V.m_y = y
       V.m_z = z
       V.m_w = w
     endif
   endfunc
 
   ; Vector Dot Product
   ; <p>
   ; This gives the cosine of the angle between the vectors.
   ;
   ; @param V the second vector in the dot product
   ; @return the dot product
   float func Dot(Vector V)
     return V.m_x*m_x+V.m_y*m_y+V.m_z*m_z+V.m_w*m_w
   endfunc
 
   ; Vector Cross Product (3D)
   ; <p>
   ; This gives the vector cross product, which given two 3D vectors will produce a third vector which is at right angles to the plane containing the two vectors. Note that changing the order of the two vectors will generally result in the cross product pointing in the opposite direction:
   ; <p>
   ; V x V1 = - V1 x V
   ; <p>
   ; This version of the cross product assumes the Vector objects hold a 3D vector, and returns a 3D vector.
   ;
   ; @param V1 the second vector in the cross product
   ; @param V2 the target vector to store the cross product vector in
   func Cross(Vector V1, Vector V2)
     float x = m_y*V1.m_z-m_z*V1.m_y
     float y = m_z*V1.m_x-m_x*V1.m_z
     float z = m_x*V1.m_y-m_y*V1.m_x
     V2.m_x = x
     V2.m_y = y
     V2.m_z = z
     V2.m_w = 1
   endfunc
 
   ; Vector Cross Product (4D)
   ; <p>
   ; This gives the vector cross product, which given three 4D vectors will produce a fourth vector which is at right angles to the hyperplane containing the three vectors. Note that changing the order of the three vectors will generally result in a different orientation of the result.
   ;
   ; @param V1 the second vector in the cross product
   ; @param V2 the third vector in the cross product
   ; @param V3 the target vector to store the cross product vector in
   func Cross4D(Vector V1, Vector V2, Vector V3)
     float a = (V1.m_x * V2.m_y) - (V1.m_y * V2.m_x);
     float b = (V1.m_x * V2.m_z) - (V1.m_z * V2.m_x);
     float c = (V1.m_x * V2.m_w) - (V1.m_w * V2.m_x);
     float d = (V1.m_y * V2.m_z) - (V1.m_z * V2.m_y);
     float e = (V1.m_y * V2.m_w) - (V1.m_w * V2.m_y);
     float f = (V1.m_z * V2.m_w) - (V1.m_w * V2.m_z);
 
     float x =   (m_y * f) - (m_z * e) + (m_w * d);
     float y = - (m_x * f) + (m_z * c) - (m_w * b);
     float z =   (m_x * e) - (m_y * c) + (m_w * a);
     float w = - (m_x * d) + (m_y * b) - (m_z * a);
     V3.m_x = x
     V3.m_y = y
     V3.m_z = z
     V3.m_w = w
   endfunc
 
   ; Rotate Vector in the YZ plane.
   ;
   ; @param theta angle to rotate, in radians
   ; @param V target vector to store resulting rotated vector in
   func RotYZ(float theta, Vector V)
     theta = theta*#pi/180
     float y = m_y*cos(theta) - m_z*sin(theta)
     float z = m_y*sin(theta) + m_z*cos(theta)
     V.m_y = y
     V.m_z = z
     V.m_x = m_x
     V.m_w = m_w
   endfunc
 
   ; Rotate Vector in the ZX plane.
   ;
   ; @param theta angle to rotate, in radians
   ; @param V target vector to store resulting rotated vector in
   func RotZX(float theta, Vector V)
     theta = theta*#pi/180
     float z = m_z*cos(theta) - m_x*sin(theta)
     float x = m_z*sin(theta) + m_x*cos(theta)
     V.m_z = z
     V.m_x = x
     V.m_y = m_y
     V.m_w = m_w
   endfunc
 
   ; Rotate Vector in the XY plane.
   ;
   ; @param theta angle to rotate, in radians
   ; @param V target vector to store resulting rotated vector in
   func RotXY(float theta, Vector V)
     theta = theta*#pi/180
     float x = m_x*cos(theta) - m_y*sin(theta)
     float y = m_x*sin(theta) + m_y*cos(theta)
     V.m_x = x
     V.m_y = y
     V.m_z = m_z
     V.m_w = m_w
   endfunc
 
   ; Rotate Vector in the XW plane.
   ;
   ; @param theta angle to rotate, in radians
   ; @param V target vector to store resulting rotated vector in
   func RotXW(float theta, Vector V)
     theta = theta*#pi/180
     float x = m_x*cos(theta) - m_w*sin(theta)
     float w = m_x*sin(theta) + m_w*cos(theta)
     V.m_x = x
     V.m_w = w
     V.m_z = m_z
     V.m_y = m_y
   endfunc
 
   ; Rotate Vector in the YW plane.
   ;
   ; @param theta angle to rotate, in radians
   ; @param V target vector to store resulting rotated vector in
   func RotYW(float theta, Vector V)
     theta = theta*#pi/180
     float w = m_w*cos(theta) - m_y*sin(theta)
     float y = m_w*sin(theta) + m_y*cos(theta)
     V.m_y = y
     V.m_w = w
     V.m_x = m_x
     V.m_z = m_z
   endfunc
 
   ; Rotate Vector in the ZW plane.
   ;
   ; @param theta angle to rotate, in radians
   ; @param V target vector to store resulting rotated vector in
   func RotZW(float theta, Vector V)
     theta = theta*#pi/180
     float w = m_w*cos(theta) - m_z*sin(theta)
     float z = m_w*sin(theta) + m_z*cos(theta)
     V.m_w = w
     V.m_z = z
     V.m_x = m_x
     V.m_y = m_y
   endfunc
 
   float m_x
   float m_y
   float m_z
   float m_w
 
 default:
 }
 


Constructor Summary
Vector()
           
Vector(float x, float y, float z, float w)
          Constructor
 
Method Summary
 void Add(Vector V1, Vector V2)
          Add two Vectors.
 void Copy(Vector T)
          Copy a Vector to another Vector.
 void Cross(Vector V1, Vector V2)
          Vector Cross Product (3D)
 void Cross4D(Vector V1, Vector V2, Vector V3)
          Vector Cross Product (4D)
 float Dot(Vector V)
          Vector Dot Product
 float Get(int n)
          Get a Vector element by index.
 void Init(float x, float y, float z, float w)
          Re-initialize a Vector.
 void MConst(float c, Vector V)
          Multiply a vector by a constant.
 void Normalize(Vector V)
          Normalize a Vector.
 void RotXW(float theta, Vector V)
          Rotate Vector in the XW plane.
 void RotXY(float theta, Vector V)
          Rotate Vector in the XY plane.
 void RotYW(float theta, Vector V)
          Rotate Vector in the YW plane.
 void RotYZ(float theta, Vector V)
          Rotate Vector in the YZ plane.
 void RotZW(float theta, Vector V)
          Rotate Vector in the ZW plane.
 void RotZX(float theta, Vector V)
          Rotate Vector in the ZX plane.
 void Set(int n, float v)
          Set a Vector element by index.
 void Shift(int shift, Vector V)
          Shift Vector elements.
 float Size()
          Determine a Vector's size.
 void Sub(Vector V1, Vector V2)
          Subtract two Vectors.
 
Methods inherited from class Object
 

Constructor Detail

Vector

public Vector(float x,
              float y,
              float z,
              float w)
Constructor

Parameters:
x - x term of vector
y - y term of vector
z - z term of vector
w - w term of vector (use 1.0 if you are storing a 3D vector)

Vector

public Vector()
Method Detail

Init

public void Init(float x,
                 float y,
                 float z,
                 float w)
Re-initialize a Vector.

If you need to completely change the value of a Vector, you can use this function rather than deleting the old Vector and creating a new one. (Re-use is faster.)

Parameters:
x - x term of vector
y - y term of vector
z - z term of vector
w - w term of vector (use 1.0 if you are storing a 3D vector)

Copy

public void Copy(Vector T)
Copy a Vector to another Vector.

If you simply assign the value of one Vector to another, you actually create a reference in both variables to the same Vector object, and changes in one variable's vector will appear in the other. See the UF help file topic on "reference" ("Objects") for details. To make a true copy from one Vector to another Vector, use this function.

Parameters:
T - target vector that will receive a copy of the Vector's data

Get

public float Get(int n)
Get a Vector element by index.

Sometimes you need to access a Vector's elements by index number (0-3) rather than by name. If you do, you may use this function. Note that this is slower than accessing the member variables directly.

Parameters:
n - element number (0-3) to retrieve
Returns:
the value of the element

Set

public void Set(int n,
                float v)
Set a Vector element by index.

Sometimes you need to set a Vector's elements by index number (0-3) rather than by name. If you do, you may use this function. Note that this is slower than accessing the member variables directly.

Parameters:
n - element number (0-3) to set
v - the new value for the element

Normalize

public void Normalize(Vector V)
Normalize a Vector.

Normalization gives a unit vector. The x, y, z and w values are direction cosines.

Parameters:
V - target vector that will receive the normalized vector

Size

public float Size()
Determine a Vector's size.

Returns:
the size (or length) of the vector

Add

public void Add(Vector V1,
                Vector V2)
Add two Vectors.

Parameters:
V1 - the second vector to add
V2 - the target vector for the added vectors to be stored in

Sub

public void Sub(Vector V1,
                Vector V2)
Subtract two Vectors.

Parameters:
V1 - the second vector to subtract
V2 - the target vector for the differenced vectors to be stored in

MConst

public void MConst(float c,
                   Vector V)
Multiply a vector by a constant.

Parameters:
c - the constant to multiply by
V - the target vector for the multiplied values to be stored in

Shift

public void Shift(int shift,
                  Vector V)
Shift Vector elements.

This function shifts the vector elements so that x becomes y, y becomes z, z becomes w, and w becomes x. You may shift by any number of positions, although any multiple of four will return the elements to their starting positions. Negative values will be interpreted as shifts in reverse.

Parameters:
shift - number of places to shift (negative values shift in reverse)
V - the target vector to store the shifted vector in

Dot

public float Dot(Vector V)
Vector Dot Product

This gives the cosine of the angle between the vectors.

Parameters:
V - the second vector in the dot product
Returns:
the dot product

Cross

public void Cross(Vector V1,
                  Vector V2)
Vector Cross Product (3D)

This gives the vector cross product, which given two 3D vectors will produce a third vector which is at right angles to the plane containing the two vectors. Note that changing the order of the two vectors will generally result in the cross product pointing in the opposite direction:

V x V1 = - V1 x V

This version of the cross product assumes the Vector objects hold a 3D vector, and returns a 3D vector.

Parameters:
V1 - the second vector in the cross product
V2 - the target vector to store the cross product vector in

Cross4D

public void Cross4D(Vector V1,
                    Vector V2,
                    Vector V3)
Vector Cross Product (4D)

This gives the vector cross product, which given three 4D vectors will produce a fourth vector which is at right angles to the hyperplane containing the three vectors. Note that changing the order of the three vectors will generally result in a different orientation of the result.

Parameters:
V1 - the second vector in the cross product
V2 - the third vector in the cross product
V3 - the target vector to store the cross product vector in

RotYZ

public void RotYZ(float theta,
                  Vector V)
Rotate Vector in the YZ plane.

Parameters:
theta - angle to rotate, in radians
V - target vector to store resulting rotated vector in

RotZX

public void RotZX(float theta,
                  Vector V)
Rotate Vector in the ZX plane.

Parameters:
theta - angle to rotate, in radians
V - target vector to store resulting rotated vector in

RotXY

public void RotXY(float theta,
                  Vector V)
Rotate Vector in the XY plane.

Parameters:
theta - angle to rotate, in radians
V - target vector to store resulting rotated vector in

RotXW

public void RotXW(float theta,
                  Vector V)
Rotate Vector in the XW plane.

Parameters:
theta - angle to rotate, in radians
V - target vector to store resulting rotated vector in

RotYW

public void RotYW(float theta,
                  Vector V)
Rotate Vector in the YW plane.

Parameters:
theta - angle to rotate, in radians
V - target vector to store resulting rotated vector in

RotZW

public void RotZW(float theta,
                  Vector V)
Rotate Vector in the ZW plane.

Parameters:
theta - angle to rotate, in radians
V - target vector to store resulting rotated vector in