//*********************************************************************************
// 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;


import java.lang.Math;


/**********************************************************************************
Simple representation of a vector in a 3D space, based on doubles.

Represented as 3 doubles. @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 doubles. */ public Vector3(double a0, double a1, double a2){ x0 = a0; x1 = a1; x2 = a2; }// end Vector3 /** Constructor from an array containing 3 doubles. @throws IllegalArgumentException if coords.length != 3. */ 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 : (x0, x1, x2). */ 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 such as b = M a. */ 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 such as b = k a. */ 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 Vector3 c such as c = a - 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 Vector3 c such as c = a + 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 Vector3 b such as b = -a. */ public static Vector3 negate(Vector3 a){ return new Vector3(-a.x0, -a.x1, -a.x2); }// end negate /** Returns a copy of a Vector3. */ public static Vector3 doClone(Vector3 a){ return a; }// end doClone }//end class Vector3

Ads help maintain this website.