The Vector3D class from The Nature of Code course is now available as a library. To use with Processing, simply:

  • download noc.zip
  • Take the zip, and decompress it into the processing libraries folder, you should then have a directory structure that looks like: /Applications/Processing 00xx/libraries/noc/library/
  • Import the library, i.e.: “import noc.*;”
  • Here is the JavaDoc, now with comments!

    Class Vector3D

    java.lang.Object
      extended bynoc.Vector3D
    

    public class Vector3D extends java.lang.Object

    A class to describe a two or three dimensional vector.


    Field Summary
     float x

              The x component of the vector.

     float y

              The y component of the vector.

     float z

              The z component of the vector.

     

    Constructor Summary
    Vector3D ()

              Constructor for an empty vector: x, y, and z are set to 0.

    Vector3D (float x_,float y_)

              Constructor for a 2D vector: z coordinate is set to 0.

    Vector3D (float x_,float y_,float z_)

              Constructor for a 3D vector.

     

    Method Summary
     void add (Vector3D v)

              Add a vector to this vector

    static Vector3D add (Vector3D v1,Vector3D v2)

              Add two vectors

    static float angleBetween (Vector3D v1,Vector3D v2)

              Calculate the angle between two vectors, using the dot product

     Vector3D copy ()

              Copy the vector

    static Vector3D copy (Vector3D v)

              Copy the vector

     Vector3D cross (Vector3D v)

              Calculate the cross product with another vector

    static float distance (Vector3D v1,Vector3D v2)

              Calculate the Euclidean distance between two points (considering a point as a vector object)

     void div (float n)

              Divide this vector by a scalar

    static Vector3D div (Vector3D v1,float n)

              Divide a vector by a scalar

     float dot (Vector3D v)

              Calculate the dot product with another vector

     float heading2D ()

              Calculate the angle of rotation for this vector (only 2D vectors)

     void limit (float max)

              Limit the magnitude of this vector

     float magnitude ()

              Calculate the magnitude (length) of the vector

     void mult (float n)

              Multiply this vector by a scalar

    static Vector3D mult (Vector3D v1,float n)

              Multiply a vector by a scalar

     void normalize ()

              Normalize the vector to length 1 (make it a unit vector)

     void setX (float x_)

              Set the x coordinate.

     void setXYZ (float x_,float y_,float z_)

              Set x,y, and z coordinates.

     void setXYZ (Vector3D v)

              Set x,y, and z coordinates from a Vector3D object.

     void setY (float y_)

              Set the y coordinate.

     void setZ (float z_)

              Set the z coordinate.

     void sub (Vector3D v)

              Subtract a vector from this vector

    static Vector3D sub (Vector3D v1,Vector3D v2)

              Subtract one vector from another

     

    Methods inherited from class java.lang.Object
    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

     

    Field Detail


    x

    public float x 

    The x component of the vector.



    y

    public float y 

    The y component of the vector.



    z

    public float z 

    The z component of the vector.

    Constructor Detail


    Vector3D

    public Vector3D (float x_,
                    float y_,
                    float z_)

    Constructor for a 3D vector.

    Parameters: x_ - the x coordinate. y_ - the y coordinate. z_ - the y coordinate.



    Vector3D

    public Vector3D (float x_,
                    float y_)

    Constructor for a 2D vector: z coordinate is set to 0.

    Parameters: x_ - the x coordinate. y_ - the y coordinate.



    Vector3D

    public Vector3D ()

    Constructor for an empty vector: x, y, and z are set to 0.

    Method Detail


    setX

    public void setX (float x_)

    Set the x coordinate.

    Parameters: x_ - the x coordinate.



    setY

    
    public void setY (float y_)

    Set the y coordinate.

    Parameters: y_ - the y coordinate.



    setZ

    public void setZ (float z_)

    Set the z coordinate.

    Parameters: z_ - the z coordinate.



    setXYZ

    public void setXYZ (float x_,
                       float y_,
                       float z_)

    Set x,y, and z coordinates.

    Parameters: x_ - the x coordinate. y_ - the y coordinate. z_ - the z coordinate.



    setXYZ

    public void setXYZ (Vector3D v)

    Set x,y, and z coordinates from a Vector3D object.

    Parameters: v - the Vector3D object to be copied



    magnitude

    public float magnitude ()

    Calculate the magnitude (length) of the vector

    Returns: the magnitude of the vector



    copy

    public Vector3D copy ()

    Copy the vector

    Returns: a copy of the vector



    copy

    public static Vector3D copy (Vector3D v)

    Copy the vector

    Parameters: v - the vector to be copied
    Returns: a copy of the vector



    add

    public void add (Vector3D v)

    Add a vector to this vector

    Parameters: v - the vector to be added



    sub

    public void sub (Vector3D v)

    Subtract a vector from this vector

    Parameters: v - the vector to be subtracted



    mult

    public void mult (float n)

    Multiply this vector by a scalar

    Parameters: n - the value to multiply by



    div

    
    public void div (float n)

    Divide this vector by a scalar

    Parameters: n - the value to divide by



    dot

    public float dot (Vector3D v)

    Calculate the dot product with another vector

    Returns: the dot product



    cross

    public Vector3D cross (Vector3D v)

    Calculate the cross product with another vector

    Returns: the cross product



    normalize

    public void normalize ()

    Normalize the vector to length 1 (make it a unit vector)



    limit

    public void limit (float max)

    Limit the magnitude of this vector

    Parameters: max - the maximum length to limit this vector



    heading2D

    
    public float heading2D ()

    Calculate the angle of rotation for this vector (only 2D vectors)

    Returns: the angle of rotation


    add

    public static Vector3D add (Vector3D v1, Vector3D v2)

    Add two vectors

    Parameters: v1 - a vector v2 - another vector
    Returns: a new vector that is the sum of v1 and v2



    sub

    public static Vector3D sub (Vector3D v1, Vector3D v2)

    Subtract one vector from another

    Parameters: v1 - a vector v2 - another vector

    Returns: a new vector that is v1 - v2



    div

    public static Vector3D div (Vector3D v1, float n)

    Divide a vector by a scalar

    Parameters: v1 - a vector n - scalar
    Returns: a new vector that is v1 / n



    mult

    public static Vector3D mult (Vector3D v1, float n)

    Multiply a vector by a scalar

    Parameters: v1 - a vector n - scalar
    Returns: a new vector that is v1 * n



    distance

    public static float distance (Vector3D v1, Vector3D v2)

    Calculate the Euclidean distance between two points (considering a point as a vector object)

    Parameters: v1 - a vectorv2 - another vector

    Returns: the Euclidean distance between v1 and v2



    angleBetween

    public static float angleBetween (Vector3D v1, Vector3D v2)

    Calculate the angle between two vectors, using the dot product

    Parameters: v1 - a vector v2 - another vector
    Returns: the angle between the vectors






    3 Responses to “Vector3D Library”  

    1. 1 han seung ku

      thank you

    1. 1 Create Digital Motion » New Processing Libraries Added: Vector3D, xmlrpclib, 2 GUIs and ID3
    2. 2 all manner of distractions » Blog Archive » Supernova


    Leave a Reply