JEphem Informatic Trail tig source code Vector3.java
//*********************************************************************************
// class tig.maths.Vector3
// Software released under the General Public License (version 2 or later), available at
// http://www.gnu.org/copyleft/gpl.html
//*********************************************************************************

package tig.maths;
/**********************************************************************************
Simple representation of a vector in a 3D space, based on <CODE>double</CODE>s.
<BR>Represented as 3 <CODE>double</CODE>s.
@author Thierry Graff
@history jan 18 2002 : creation
**********************************************************************************/
public class Vector3{

  //=================================================================================
  //                            CONSTANTS
  //=================================================================================
  /** First coordinate of this vector. */
  public double x0;
  /** Second coordinate of this vector. */
  public double x1;
  /** Third coordinate of this vector. */
  public double x2;
					 
  //=================================================================================
  //                            CONSTRUCTORS
  //=================================================================================
  /** Constructor from 3 <CODE>double</CODE>s. */
  public Vector3(double a0, double a1, double a2){
    x0 = a0; x1 = a1; x2 = a2; 
  }// end Vector3

  /** Constructor from an array containing 3 <CODE>double</CODE>s.
  @throws IllegalArgumentException if <CODE>coords.length != 3</CODE>.
  */
  public Vector3(double[] coords){
    if (coords.length != 3)
      throw new IllegalArgumentException("Array passed to Vector3 constructor "
                                       + "must have 3 elements");
    x0 = coords[0]; x1 = coords[1]; x2 = coords[2]; 
  }// end Vector3

  //=================================================================================
  //                            PUBLIC METHODS
  //=================================================================================

  /** Returns a String representation of this Vector. The form is : <CODE>(x0, x1, x2)</CODE>. */
  public String toString(){
    return "(" + this.x0 + ", " + this.x1 + ", " + this.x2 + ")";
  }// end toString()

  //=================================================================================
  //                            STATIC METHODS
  //=================================================================================

  /** Returns the norm of the vector passed in parameter. */
  public static double norm(Vector3 v){
    return Math.sqrt(v.x0*v.x0 + v.x1*v.x1 + v.x2*v.x2);
  }// end norm()

  /** Multiplication of a vector by a matrix ; returns a vector <B>b</B> such as <B>b = M a</B>. */
  public static Vector3 mul(Matrix3 M, Vector3 a){
    return new Vector3(M.m00*a.x0 + M.m01*a.x1 + M.m02*a.x2,
                        M.m10*a.x0 + M.m11*a.x1 + M.m12*a.x2,
                        M.m20*a.x0 + M.m21*a.x1 + M.m22*a.x2);
  }// end mul(Matrix3, Vector3)

  /** Multiplication of a vector by a scalar ; returns a vector <B>b</B> such as <B>b = k a</B>. */
  public static Vector3 mul(double k, Vector3 a){
    return new Vector3(k*a.x0, k*a.x1, k*a.x2);
  }// end mul(double, Vector3)

  /** Returns a <CODE>Vector3</CODE> <B>c</B> such as <B>c = a - b</B>. */
  public static Vector3 sub(Vector3 a, Vector3 b){
    return new Vector3(a.x0 - b.x0, a.x1 - b.x1, a.x2 - b.x2);
  }// end sub
  
  /** Returns a <CODE>Vector3</CODE> <B>c</B> such as <B>c = a + b</B>. */
  public static Vector3 add(Vector3 a, Vector3 b){
    return new Vector3(a.x0 + b.x0, a.x1 + b.x1, a.x2 + b.x2);
  }// end add
  
  /** Returns a <CODE>Vector3</CODE> <B>b</B> such as <B>b = -a</B>. */
  public static Vector3 negate(Vector3 a){
    return new Vector3(-a.x0, -a.x1, -a.x2);
  }// end negate
  
  /** Returns a copy of a <CODE>Vector3</CODE>. */
  public static Vector3 doClone(Vector3 a){
    return a;
  }// end doClone
  
}//end class Vector3