/** * Basic core vector class, dot products, cross products * and basic vector operations. * * @author Daniel Staudigel */ public class Vector { /** * We are using public fields here for performance */ public double x,y,z; public static final Vector zero = new Vector(0,0,0); public static final Vector xAxis = new Vector(1,0,0); public static final Vector yAxis = new Vector(0,1,0); public static final Vector zAxis = new Vector(0,0,1); /** * Vector(): Basic stuff * */ public Vector() { x = 0; y = 0; z = 0; } /** * Creates a 3D double vector with given (x,y,z) * @param px Input X value * @param py Input Y value * @param pz Input Z value */ public Vector(double px,double py,double pz) { x = px; y = py; z = pz; } /** * Copies X,Y,Z, information from another Vector * @param copy vector to copy */ public Vector(Vector copy) { x = copy.x; y = copy.y; z = copy.z; } /** * Returns a copy of the current vector * @return copy of the current vector */ public Vector copy() { return new Vector(this); } /** * Performs a dot product * @param vec vector to dot with * @return the dot product */ public double dot(Vector vec) { return x*vec.x + y*vec.y + z*vec.z; } /** * Performs a dot product * @param v1 Vector 1 * @param v2 Vector 2 * @return Dot product of v1 and v2 */ static public double dot(Vector v1,Vector v2) { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; } /** * Performs the cross product * @param vec Vector to perform with * @return the Vector cross product */ public Vector cross(Vector vec) { return new Vector(y*vec.z - z*vec.y, z*vec.x - x*vec.z, x*vec.y - y*vec.x); } public static Vector cross(Vector v1, Vector v2) { return new Vector(v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x); } /** * Returns the square of the length of the vector * @return square of the length */ public double lengthSquared() { return x*x+y*y+z*z; } /** * Returns the length of the vector * @return length of the vector */ public double length() { return Math.sqrt(x*x+y*y+z*z); } /** * Normalizes the vector and returns the length * @return Length of vector */ public double normalize() { double length,square = x*x+y*y+z*z; if(square > 0) { length = Math.sqrt(square); x /= length; y /= length; z /= length; } else { length = 0; } return length; } static public Vector normalize(Vector v) { double length = Math.sqrt(v.x*v.x + v.y*v.y + v.z*v.z); if(length>0) { return new Vector(v.x/length,v.y/length,v.z/length); } else { return new Vector(v); } } /** * Adds a vector to the target * @param v other vector * @return vector sum of target and argument */ public void add(Vector v) { x+=v.x; y+=v.y; z+=v.z; } /** * Adds two vectors, returns new vector * @param v1 First vector * @param v2 Second vector * @return vector sum of v1 and v2 */ static public Vector add(Vector v1,Vector v2) { return new Vector(v1.x+v2.x,v1.y+v2.y,v1.z+v2.z); } /** * Sets the vector components */ public void set(double px,double py,double pz) { x = px; y = py; z = pz; } /** * Adds a vector (scaled by some factor) to the target * @param v vector to add * @param s scalar to multiply v by before adding * @return x + v.x*s */ public void addScaled(Vector v,double s) { x+=v.x*s; y+=v.y*s; z+=v.z*s; } /** * Adds two vectors, the second one scaled by some factor * @param v1 First vector * @param v2 Second vector * @param s scale factor * @param scaled vector sum */ static public Vector addScaled(Vector v1,Vector v2,double s) { return new Vector(v1.x+v2.x*s,v1.y+v2.y*s,v1.z+v2.z*s); } /** * Subtracts a vector from the target * @param v another vector */ public void sub(Vector v) { x-=v.x; y-=v.y; z-=v.z; } /** * Subtract two vectors * @param v1 First vector * @param v2 Second vector * @return v1.x-v2.x */ static public Vector sub(Vector v1,Vector v2) { return new Vector(v1.x-v2.x,v1.y-v2.y,v1.z-v2.z); } /** * Sets the vector's contents to v * @param v */ public void set(Vector v) { x = v.x; y = v.y; z = v.z; } /** * Scalar multiplication * @param f scale factor * @return result vector */ public void mul(double f) { x*=f; y*=f; z*=f; } /** * Scale a vector by f and return a new vector * @param v Vector * @param s Scale factor * @return v*s */ static public Vector mul(Vector v,double s) { return new Vector(v.x*s,v.y*s,v.z*s); } /** * Component division r.x = this.x / v.x; * @param v vector to divide components * @return result vector */ public void componentDiv(Vector v) { x/=v.x; y/=v.y; z/=v.z; } /** * Static component division * @param v1 Vector 1 * @param v2 Vector 2 * @return Vector */ static public Vector componentDiv(Vector v1,Vector v2) { return new Vector( v1.x / v2.x, v1.y / v2.y, v1.z / v2.z); } public String toString() { return "V("+x+","+y+","+z+")"; } }