package matrix; import java.lang.Math; public class Vector3D extends Vector { public double x() { return component(0); } public double y() { return component(1); } public double z() { return component(2); } public Vector3D() { super(3); //{{INIT_CONTROLS //}} } public Vector3D(double x, double y, double z) { super(3); setComponent(0, x); setComponent(1, y); setComponent(2, z); } public Vector3D(Vector v) throws VectorDimensionError { super(3); if (v.size() != 3) throw new VectorDimensionError(this, v); setComponent(0, v.component(0)); setComponent(1, v.component(1)); setComponent(2, v.component(2)); } // promote a Vector2D to a Vector3D with z = 0. public Vector3D(Vector2D v) throws VectorDimensionError { super(3); setComponent(0, v.x()); setComponent(1, v.y()); setComponent(2, 0); } /* vector addition */ public Vector3D add(Vector3D v2) { return new Vector3D(x() + v2.x(), y() + v2.y(), z() + v2.z()); } public Vector add(Vector v2) { return add((Vector3D)v2); } public static Vector3D add(Vector3D v1, Vector3D v2) { return v1.add(v2); } /* vector subtraction */ public Vector3D subtract(Vector3D v2) { return new Vector3D(x() - v2.x(), y() - v2.y(), z() - v2.z()); } public Vector subtract(Vector v2) { return subtract((Vector3D)v2); } public static Vector3D subtract(Vector3D v1, Vector3D v2) { return v1.subtract(v2); } /* scalar multiplication */ public static Vector3D mult(Vector3D v, double a) { return new Vector3D(a * v.x(), a * v.y(), a * v.z()); } public Vector mult(double a) { return mult(this, a); } /* vector dot product (component-wise multiplication) */ public double dotProduct(Vector3D v2) { return x() * v2.x() + y() * v2.y() + z() * v2.z(); } public double dotProduct(Vector v2) { return dotProduct((Vector3D)v2); } public static double dotProduct(Vector3D v1, Vector3D v2) { return v1.dotProduct(v2); } /* cross product yielding a vector perpindicular to v1 and v2 with length equal to the area of the parallelogram between them. */ public Vector3D crossProduct(Vector3D v2) { return new Vector3D(y() * v2.z() - z() * v2.y(), z() * v2.x() - x() * v2.z(), x() * v2.y() - y() * v2.x()); } public Vector3D crossProduct(Vector v2) { return crossProduct((Vector3D)v2); } public static Vector3D crossProduct(Vector3D v1, Vector3D v2) { return v1.crossProduct(v2); } /* default casting for superclass functions */ public Vector3D normalized() { return (Vector3D)(normalized(this)); } public Vector3D rescaled(double newLength) { return (Vector3D)(super.rescaled(this, newLength)); } public Vector3D reflected(Vector3D normal) { return (Vector3D)(super.reflected(normal)); } //{{DECLARE_CONTROLS //}} }