/** * Converts this vector into a unit vector by dividing it internally by its length. If the length is 0, (ie, if the * vector is 0, 0) then no action is taken. * * @return this vector for chaining */ public Vector2 normalizeLocal() { final double lengthSq = lengthSquared(); if (Math.abs(lengthSq) > MathUtils.EPSILON) { return multiplyLocal(MathUtils.inverseSqrt(lengthSq)); } return this; }
/** * Converts this vector into a unit vector by dividing it internally by its length. If the length is 0, (ie, if the * vector is 0, 0, 0, 0) then no action is taken. * * @return this vector for chaining */ public Vector4 normalizeLocal() { final double lengthSq = lengthSquared(); if (Math.abs(lengthSq) > MathUtils.EPSILON) { return multiplyLocal(MathUtils.inverseSqrt(lengthSq)); } return this; }
/** * Converts this vector into a unit vector by dividing it internally by its length. If the length is 0, (ie, if the * vector is 0, 0, 0, 0) then no action is taken. * * @return this vector for chaining */ public Vector4 normalizeLocal() { final double lengthSq = lengthSquared(); if (Math.abs(lengthSq) > MathUtils.EPSILON) { return multiplyLocal(MathUtils.inverseSqrt(lengthSq)); } return this; }
/** * Converts this vector into a unit vector by dividing it internally by its length. If the length is 0, (ie, if the * vector is 0, 0, 0) then no action is taken. * * @return this vector for chaining */ public Vector3 normalizeLocal() { final double lengthSq = lengthSquared(); if (Math.abs(lengthSq) > MathUtils.EPSILON) { return multiplyLocal(MathUtils.inverseSqrt(lengthSq)); } return this; }
/** * Converts this vector into a unit vector by dividing it internally by its length. If the length is 0, (ie, if the * vector is 0, 0, 0) then no action is taken. * * @return this vector for chaining */ public Vector3 normalizeLocal() { final double lengthSq = lengthSquared(); if (Math.abs(lengthSq) > MathUtils.EPSILON) { return multiplyLocal(MathUtils.inverseSqrt(lengthSq)); } return this; }
/** * Converts this vector into a unit vector by dividing it internally by its length. If the length is 0, (ie, if the * vector is 0, 0) then no action is taken. * * @return this vector for chaining */ public Vector2 normalizeLocal() { final double lengthSq = lengthSquared(); if (Math.abs(lengthSq) > MathUtils.EPSILON) { return multiplyLocal(MathUtils.inverseSqrt(lengthSq)); } return this; }
/** * 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, 0) then a new vector (0, 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, 0 if this unit is 0 length) */ @Override public Vector3 normalize(final Vector3 store) { final double lengthSq = lengthSquared(); if (Math.abs(lengthSq) > MathUtils.EPSILON) { return multiply(MathUtils.inverseSqrt(lengthSq), store); } return store != null ? store.set(Vector3.ZERO) : new Vector3(Vector3.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); }
/** * 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, 0) then a new vector (0, 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, 0 if this unit is 0 length) */ @Override public Vector3 normalize(final Vector3 store) { final double lengthSq = lengthSquared(); if (Math.abs(lengthSq) > MathUtils.EPSILON) { return multiply(MathUtils.inverseSqrt(lengthSq), store); } return store != null ? store.set(Vector3.ZERO) : new Vector3(Vector3.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, 0, 0) then a new vector (0, 0, 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, 0, 0 if this unit is 0 length) */ @Override public Vector4 normalize(final Vector4 store) { final double lengthSq = lengthSquared(); if (Math.abs(lengthSq) > MathUtils.EPSILON) { return multiply(MathUtils.inverseSqrt(lengthSq), store); } return store != null ? store.set(Vector4.ZERO) : new Vector4(Vector4.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, 0, 0) then a new vector (0, 0, 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, 0, 0 if this unit is 0 length) */ @Override public Vector4 normalize(final Vector4 store) { final double lengthSq = lengthSquared(); if (Math.abs(lengthSq) > MathUtils.EPSILON) { return multiply(MathUtils.inverseSqrt(lengthSq), store); } return store != null ? store.set(Vector4.ZERO) : new Vector4(Vector4.ZERO); }
@Test public void testNormalize() { final Vector4 vec1 = new Vector4(2, 1, 3, -1); assertTrue(vec1.length() == Math.sqrt(15)); final Vector4 vec2 = vec1.normalize(null); final double invLength = MathUtils.inverseSqrt(2 * 2 + 1 * 1 + 3 * 3 + -1 * -1); assertEquals(new Vector4(2 * invLength, 1 * invLength, 3 * invLength, -1 * invLength), vec2); vec1.normalizeLocal(); assertEquals(new Vector4(2 * invLength, 1 * invLength, 3 * invLength, -1 * invLength), vec1); vec1.zero(); vec1.normalize(vec2); assertEquals(vec1, vec2); // ensure no exception thrown vec1.normalizeLocal(); vec1.normalize(null); }
@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); }
@Test public void testNormalize() { final Vector3 vec1 = new Vector3(2, 1, 3); assertTrue(vec1.length() == Math.sqrt(14)); final Vector3 vec2 = vec1.normalize(null); final double invLength = MathUtils.inverseSqrt(2 * 2 + 1 * 1 + 3 * 3); assertEquals(new Vector3(2 * invLength, 1 * invLength, 3 * invLength), vec2); vec1.normalizeLocal(); assertEquals(new Vector3(2 * invLength, 1 * invLength, 3 * invLength), vec1); vec1.zero(); vec1.normalize(vec2); assertEquals(vec1, vec2); // ensure no exception thrown vec1.normalizeLocal(); vec1.normalize(null); }