Purpose: A utility class that can be used to perform vector arithmetic.
About the System: This class will be used by a 2D renderer and a 3D renderer (and, perhaps, other classes).
Details
Encapsulating Vectors:
double[]
to hold the elements.
Vectors of Different Size:
null
.
While the second approach is probably the best in many respects, the third will be more convenient for rendering. Hence, you must use the third approach.
For example, if the add()
method is passed to vectors,
one with 4 elements and the other with 2, it should use the first
two elements of both and return a vector with 2 elements.
Side Effects:
Attributes:
private static final double TOL = 0.000000001;
The cross() Method:
/** * Calculate the cross product of two vectors * in RxRxR * * @param a One vector * @param b The other vector * @return The cross product */ public static double[] cross(double[] a, double[] b)
For this method, both vectors must have exactly three elements.
The diff() Method:
/** * Calculate the difference between two vectors * (i.e., a-b) * * @param a One vector * @param b The other vector * @return a - b */ public static double[] diff(double[] a, double[] b)
The dot() Method:
/** * Calculate the dot/inner product of two vectors * * @param a One vector * @param b The other vector * @return a . b */ public static double dot(double[] a, double[] b)
The equals() Method:
/** * Determines if two vectors are equal (within a given tolerance) * * @param p One vector * @param q Another vector * @return true if all of the elements are the same */ public static boolean equals(double[] p, double[] q)
This method must use TOL
as the tolerance.
The norm() Method:
/** * Calculate the Euclidean norm of a vector * * @param p The vector * @return ||p|| */ public static double norm(double[] p)
The normalized() Method:
/** * Calculate the normalized version of a vector * * @param p The vector * @return p/||p|| */ public static double[] normalized(double[] p)
The perp() Method:
/** * Find a perpindicular to the given 2D vector * * @param p The vector * @return The perpendicular [-p[1], p[0]] */ public static double[] perp(double[] p)
This method must return a vector with two elements.
The sum() Method:
/** * Calculate the sum of two vectors * (i.e., a+b) * * @param a One vector * @param b The other vector * @return a + b */ public static double[] sum(double[] a, double[] b)
The times() Method:
/** * Multiply a vector by a scalar * * @param alpha The scalar * @param p The vector * @return alpha p */ public static double[] times(double alpha, double[] p)
Copyright 2007