Vec2 { public Vec2 subtract(Vec2 vector) { if (vector == null) { throw new IllegalArgumentException( Logger.logMessage(Logger.ERROR, "Vec2", "subtract", "missingVector")); } this.x -= vector.x; this.y -= vector.y; return this; } Vec2();  Vec2(double x, double y);  Vec2(Vec2 vector); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); float[] toArray(float[] result, int offset); double magnitude(); double magnitudeSquared(); double distanceTo(Vec2 vector); double distanceToSquared(Vec2 vector); Vec2 set(double x, double y); Vec2 set(Vec2 vector); Vec2 swap(Vec2 vector); Vec2 add(Vec2 vector); Vec2 subtract(Vec2 vector); Vec2 multiply(double scalar); Vec2 multiplyByMatrix(Matrix3 matrix); Vec2 divide(double divisor); Vec2 negate(); Vec2 normalize(); double dot(Vec2 vector); Vec2 mix(Vec2 vector, double weight); public double x; public double y; }
@Test public void testSubtract() throws Exception { final double x1 = 3d; final double y1 = 4d; final double x2 = 5d; final double y2 = 6d; Vec2 u = new Vec2(x1, y1); Vec2 v = new Vec2(x2, y2); Vec2 w = u.subtract(v); assertEquals("u.x", x1 - x2, u.x, 0d); assertEquals("u.y", y1 - y2, u.y, 0d); assertEquals("v.x", x2, v.x, 0d); assertEquals("v.y", y2, v.y, 0d); assertEquals("w == u", u, w); }
@Test public void testSubtract_NaN() throws Exception { Vec2 u = new Vec2(); Vec2 v = new Vec2(); assertTrue("1st Nan", hasNaN(u.subtract(new Vec2(Double.NaN, 4d)))); assertTrue("2nd Nan", hasNaN(v.subtract(new Vec2(3d, Double.NaN)))); }
@Test(expected = IllegalArgumentException.class) public void testSubtract_WithNull() throws Exception { Vec2 u = new Vec2(); u.subtract(null); fail("Expected an IllegalArgumentException to be thrown."); }