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

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

Ads help maintain this website.