package matrix; import java.lang.Math; public class Vector2D extends Vector { public double x() { return component(0); } public double y() { return component(1); } public Vector2D() { super(2); //{{INIT_CONTROLS //}} } public Vector2D(double x, double y) { super(2); setComponent(0, x); setComponent(1, y); } public Vector2D(Vector v) throws VectorDimensionError { super(2); if (v.size() != 2) throw new VectorDimensionError(this, v); setComponent(0, v.component(0)); setComponent(1, v.component(1)); } /* vector addition */ public Vector2D add(Vector2D v2) { return new Vector2D(x() + v2.x(), y() + v2.y()); } public Vector add(Vector v2) { return add((Vector2D)v2); } public static Vector2D add(Vector2D v1, Vector2D v2) { return v1.add(v2); } /* vector subtraction */ public Vector2D subtract(Vector2D v2) { return new Vector2D(x() - v2.x(), y() - v2.y()); } public Vector subtract(Vector v2) { return subtract((Vector2D)v2); } public static Vector2D subtract(Vector2D v1, Vector2D v2) { return v1.subtract(v2); } /* scalar multiplication */ public static Vector2D mult(Vector2D v, double a) { return new Vector2D(a * v.x(), a * v.y()); } public Vector mult(double a) { return mult(this, a); } /* vector dot product (component-wise multiplication) */ public double dotProduct(Vector2D v2) { return x() * v2.x() + y() * v2.y(); } public double dotProduct(Vector v2) { return dotProduct((Vector2D)v2); } public static double dotProduct(Vector2D v1, Vector2D v2) { return v1.dotProduct(v2); } /* cross product between two Vector2D's giving a scalar representing a vector perpindicular (out of 2D plane) */ public double crossProduct(Vector2D v2) { return x() * v2.y() - y() * v2.x(); } public static double crossProduct(Vector2D v1, Vector2D v2) { return v1.crossProduct(v2); } /* cross products between vector on plane and scalar represending a vector pointing out of plane */ public Vector2D crossProduct(double z) { return new Vector2D(y() * z, -x() * z); } public static Vector2D crossProduct(Vector2D v, double z) { return v.crossProduct(z); } public static Vector2D crossProduct(double z, Vector2D v) { return v.crossProduct(-z); } /* default casting for superclass functions */ public Vector2D normalized() { return (Vector2D)(normalized(this)); } public Vector2D rescaled(double newLength) { return (Vector2D)(rescaled(this, newLength)); } public Vector2D reflected(Vector2D normal) { return (Vector2D)(super.reflected(normal)); } //{{DECLARE_CONTROLS //}} }