/** * Normalize a Vector2 in-buffer. * * @param buf * the buffer to find the Vector2 within * @param index * the position (in terms of vectors, not floats) of the vector to normalize */ public static void normalizeVector2(final FloatBuffer buf, final int index) { final Vector2 temp = Vector2.fetchTempInstance(); populateFromBuffer(temp, buf, index); temp.normalizeLocal(); setInBuffer(temp, buf, index); Vector2.releaseTempInstance(temp); }
/** * Normalize a Vector2 in-buffer. * * @param buf * the buffer to find the Vector2 within * @param index * the position (in terms of vectors, not floats) of the vector to normalize */ public static void normalizeVector2(final FloatBuffer buf, final int index) { final Vector2 temp = Vector2.fetchTempInstance(); populateFromBuffer(temp, buf, index); temp.normalizeLocal(); setInBuffer(temp, buf, index); Vector2.releaseTempInstance(temp); }
@Test public void testNormalize() { final Vector2 vec1 = new Vector2(2, 1); assertTrue(vec1.length() == Math.sqrt(5)); final Vector2 vec2 = vec1.normalize(null); final double invLength = MathUtils.inverseSqrt(2 * 2 + 1 * 1); assertEquals(new Vector2(2 * invLength, 1 * invLength), vec2); vec1.normalizeLocal(); assertEquals(new Vector2(2 * invLength, 1 * invLength), vec1); vec1.zero(); vec1.normalize(vec2); assertEquals(vec1, vec2); // ensure no exception thrown vec1.normalizeLocal(); vec1.normalize(null); }