/** * Checks if the length of this {@link Vector3} is smaller than the specified margin. * * @param margin double The desired margin of error for the test. * * @return boolean True if this {@link Vector3}'s length is smaller than the margin specified. */ public boolean isZero(double margin) { return (length2() <= margin * margin); }
/** * Computes the squared Euclidean length of the arbitrary vector components passed in. * * @param v {@link Vector3} The {@link Vector3} to calculate the length of. * * @return double The squared Euclidean length. */ public static double length2(@NonNull Vector3 v) { return length2(v.x, v.y, v.z); }
/** * Computes the Euclidean length of the arbitrary vector components passed in. * * @param x double The x component. * @param y double The y component. * @param z double The z component. * * @return double The Euclidean length. */ public static double length(double x, double y, double z) { return Math.sqrt(length2(x, y, z)); }
/** * Checks if this {@link Vector3} is of unit length with a specified * margin of error. * * @param margin double The desired margin of error for the test. * * @return boolean True if this {@link Vector3} is of unit length. */ public boolean isUnit(double margin) { return Math.abs(length2() - 1) < margin * margin; }
/** * Projects the specified {@link Vector3} onto this one and sets this {@link Vector3} * to the result. * * @param v {@link Vector3} to be projected. * * @return A reference to this {@link Vector3} to facilitate chaining. */ @NonNull public Vector3 project(@NonNull Vector3 v) { double d = dot(v); double d_div = d / length2(); return multiply(d_div); }
/** * Projects {@link Vector3} u onto {@link Vector3} v and creates a new * {@link Vector3} for the result. * * @param u {@link Vector3} to be projected. * @param v {@link Vector3} the {@link Vector3} to be projected on. * * @return {@link Vector3} The result of the projection. */ @NonNull public static Vector3 projectAndCreate(@NonNull Vector3 u, @NonNull Vector3 v) { double d = u.dot(v); double d_div = d / v.length2(); return v.clone().multiply(d_div); }
@Test public void testLength2DoublesXyz() { final double l2 = Vector3.length2(1d, 2d, 3d); assertEquals(14d, l2, 1e-14); }
@Test public void testLength2FromVector3() { final Vector3 v = new Vector3(1d, 2d, 3d); final double l2 = Vector3.length2(v); assertEquals(14d, l2, 1e-14); }
@Test public void testLength2FromSelf() { final Vector3 v = new Vector3(1d, 2d, 3d); final double l2 = v.length2(); assertEquals(14d, l2, 1e-14); }
/** * Computes the squared Euclidean length of the arbitrary vector components passed in. * * @param v {@link Vector3} The {@link Vector3} to calculate the length of. * * @return double The squared Euclidean length. */ public static double length2(Vector3 v) { return length2(v.x, v.y, v.z); }
/** * Checks if the length of this {@link Vector3} is smaller than the specified margin. * * @param margin double The desired margin of error for the test. * * @return boolean True if this {@link Vector3}'s length is smaller than the margin specified. */ public boolean isZero(final double margin) { return (length2() < margin * margin); }
/** * Checks if this {@link Vector3} is of unit length with a specified * margin of error. * * @param margin double The desired margin of error for the test. * * @return boolean True if this {@link Vector3} is of unit length. */ public boolean isUnit(final double margin) { return Math.abs(length2() - 1) < margin * margin; }
/** * Computes the Euclidean length of the arbitrary vector components passed in. * * @param x double The x component. * @param y double The y component. * @param z double The z component. * * @return double The Euclidean length. */ public static double length(double x, double y, double z) { return Math.sqrt(length2(x, y, z)); }
/** * Projects the specified {@link Vector3} onto this one and sets this {@link Vector3} * to the result. * * @param v {@link Vector3} to be projected. * * @return A reference to this {@link Vector3} to facilitate chaining. */ public Vector3 project(Vector3 v) { double d = dot(v); double d_div = d / length2(); return multiply(d_div); }
/** * Projects {@link Vector3} v1 onto {@link Vector3} v2 and creates a new * {@link Vector3} for the result. * * @param v1 {@link Vector3} to be projected. * @param v2 {@link Vector3} the {@link Vector3} to be projected on. * * @return {@link Vector3} The result of the projection. */ public static Vector3 projectAndCreate(Vector3 v1, Vector3 v2) { double d = v1.dot(v2); double d_div = d / v2.length2(); return v2.clone().multiply(d_div); }