/** * @param store * the vector to store the result in for return. If null, a new vector object is created and returned. * @return same as multiply(-1, store) */ @Override public Vector2 negate(final Vector2 store) { return multiply(-1, store); }
@Test public void testMultiply() { final Vector2 vec1 = new Vector2(1, -1); final Vector2 vec2 = vec1.multiply(2.0, null); final Vector2 vec2B = vec1.multiply(2.0, new Vector2()); assertEquals(new Vector2(2.0, -2.0), vec2); assertEquals(new Vector2(2.0, -2.0), vec2B); vec2.multiplyLocal(0.5); assertEquals(new Vector2(1.0, -1.0), vec2); final Vector2 vec3 = vec1.multiply(vec2, null); final Vector2 vec3B = vec1.multiply(vec2, new Vector2()); assertEquals(Vector2.ONE, vec3); assertEquals(Vector2.ONE, vec3B); final Vector2 vec4 = vec1.multiply(2, 3, null); final Vector2 vec4B = vec1.multiply(2, 3, new Vector2()); assertEquals(new Vector2(2, -3), vec4); assertEquals(new Vector2(2, -3), vec4B); vec1.multiplyLocal(0.5, 0.5); assertEquals(new Vector2(0.5, -0.5), vec1); vec1.multiplyLocal(vec2); assertEquals(new Vector2(0.5, 0.5), vec1); }
/** * @param store * the vector to store the result in for return. If null, a new vector object is created and returned. * @return same as multiply(-1, store) */ @Override public Vector2 negate(final Vector2 store) { return multiply(-1, store); }
/** * Creates a new unit length vector from this one by dividing by length. If the length is 0, (ie, if the vector is * 0, 0) then a new vector (0, 0) is returned. * * @param store * the vector to store the result in for return. If null, a new vector object is created and returned. * @return a new unit vector (or 0, 0 if this unit is 0 length) */ @Override public Vector2 normalize(final Vector2 store) { final double lengthSq = lengthSquared(); if (Math.abs(lengthSq) > MathUtils.EPSILON) { return multiply(MathUtils.inverseSqrt(lengthSq), store); } return store != null ? store.set(Vector2.ZERO) : new Vector2(Vector2.ZERO); }
/** * Creates a new unit length vector from this one by dividing by length. If the length is 0, (ie, if the vector is * 0, 0) then a new vector (0, 0) is returned. * * @param store * the vector to store the result in for return. If null, a new vector object is created and returned. * @return a new unit vector (or 0, 0 if this unit is 0 length) */ @Override public Vector2 normalize(final Vector2 store) { final double lengthSq = lengthSquared(); if (Math.abs(lengthSq) > MathUtils.EPSILON) { return multiply(MathUtils.inverseSqrt(lengthSq), store); } return store != null ? store.set(Vector2.ZERO) : new Vector2(Vector2.ZERO); }