//*********************************************************************************
// class tig.maths.Vector2
// 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 2D space.
<BR>Represented as 2 <CODE>double</CODE>s.
@author Thierry Graff
@history apr 29 2002 : creation from Vector3
**********************************************************************************/
public class Vector2{
//=================================================================================
// CONSTANTS
//=================================================================================
/** First coordinate of this vector. */
public double x0;
/** Second coordinate of this vector. */
public double x1;
//=================================================================================
// CONSTRUCTORS
//=================================================================================
/** Constructor from 2 <CODE>double</CODE>s. */
public Vector2(double a0, double a1){
x0 = a0; x1 = a1;
}// end Vector2
/** Constructor from an array containing 2 <CODE>double</CODE>s.
@throws IllegalArgumentException if <CODE>coords.length != 2</CODE>.
*/
public Vector2(double[] coords){
if (coords.length != 2)
throw new IllegalArgumentException("Array passed to Vector2 constructor "
+ "must have 2 elements");
x0 = coords[0]; x1 = coords[1];
}// end Vector2
//=================================================================================
// PUBLIC METHODS
//=================================================================================
/** Returns a String representation of this Vector. The form is : <CODE>(x0, x1)</CODE>. */
public String toString(){
return "(" + this.x0 + ", " + this.x1 + ")";
}// end toString()
//=================================================================================
// STATIC METHODS
//=================================================================================
/** Returns the norm of the vector passed in parameter. */
public static double norm(Vector2 v){
return Math.sqrt(v.x0*v.x0 + v.x1*v.x1);
}// end norm()
/** Multiplication of a vector by a matrix ; returns a vector <B>b</B> such as <B>b = M a</B>. */
public static Vector2 mul(Matrix2 M, Vector2 a){
return new Vector2(M.m00*a.x0 + M.m01*a.x1,
M.m10*a.x0 + M.m11*a.x1);
}// end mul(Matrix2, Vector2)
/** Multiplication of a vector by a scalar ; returns a vector <B>b</B> such as <B>b = k a</B>. */
public static Vector2 mul(double k, Vector2 a){
return new Vector2(k*a.x0, k*a.x1);
}// end mul(double, Vector2)
/** Returns a <CODE>Vector2</CODE> <B>c</B> such as <B>c = a - b</B>. */
public static Vector2 sub(Vector2 a, Vector2 b){
return new Vector2(a.x0 - b.x0, a.x1 - b.x1);
}// end sub
/** Returns a <CODE>Vector2</CODE> <B>c</B> such as <B>c = a + b</B>. */
public static Vector2 add(Vector2 a, Vector2 b){
return new Vector2(a.x0 + b.x0, a.x1 + b.x1);
}// end add
/** Returns a <CODE>Vector2</CODE> <B>b</B> such as <B>b = -a</B>. */
public static Vector2 negate(Vector2 a){
return new Vector2(-a.x0, -a.x1);
}// end negate
}//end class Vector2