/* * Moonlight|3D Copyright (C) 2006 The Moonlight|3D team * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on Oct 31, 2006 */ package ml.math; import ml.core.exceptions.Exception; public class Vector2D implements Cloneable { public double X1, X2; public Vector2D() { } public Vector2D(double X1, double X2) { this.X1=X1; this.X2=X2; } /** * Clear the component values. This sets this vector equal to the * 0 vector. */ public void clear() { X1=0.0; X2=0.0; } /** * Test if this vector is mathematical equal to the given vector, * i.e. if all the components are equal. * * @param a the vector to test against * @return true if the vectors are equal */ public boolean equals(Vector2D a) { if (X1 != a.X1) { return false; } if (X2 != a.X2) { return false; } return true; } /** * Return a complete copy of this Vector3D. */ @Override public Vector2D clone() { Vector2D ret=null; try { ret = (Vector2D) super.clone(); } catch (CloneNotSupportedException e) { // cannot happen } return ret; } /** * Return the absolute value of this vector, i.e. calculate * \f$ \sqrt{x^2 + y^2 + z^2} \f$. * * @return the absolute value */ public double abs() { return Math.sqrt(X1*X1+X2*X2); } /** * Return a normalised copy of this vector. * * @return normalised vector */ public Vector2D norm() { Vector2D res=new Vector2D(); double abs=abs(); res.X1=X1/abs; res.X2=X2/abs; return res; } /** * Add the given vector to this vector and return the result. * * @param a the vector to add * @return the sum of the two vectors */ public Vector2D add(Vector2D a) { Vector2D res=new Vector2D(); res.X1=X1+a.X1; res.X2=X2+a.X2; return res; } /** * Subtract the given vector from this vector and return * the result. * * @param a the vector to subtract * @return the difference between the two vectors */ public Vector2D sub(Vector2D a) { Vector2D res=new Vector2D(); res.X1=X1-a.X1; res.X2=X2-a.X2; return res; } /** * Perform scalar multiplication with the given value. * * @param a the value with which to multiply * @return the multiplied vector */ public Vector2D multiply(double a) { Vector2D res=new Vector2D(); res.X1=X1*a; res.X2=X2*a; return res; } /** * Perform inner product between this and the given vector * and return the result. * * @param a the vector to multiply * @return the resulting value */ public double multiply(Vector2D a) { return X1*a.X1+X2*a.X2; } /** * Return the angle between this and the given vector. * * @param b the vector to compute the angle to * @return the angle between the vectors * @throws Exception */ public double angleTo(Vector2D b) throws Exception { if (abs() == 0 || b.abs() == 0) { throw new Exception("cannot calculate angle to null vector"); } double cosAngle = multiply(b) / (abs() * b.abs()); double angle = Math.acos(cosAngle); return angle; } }