Add lengthSquared & #eq

This commit is contained in:
TheMode 2021-07-03 02:24:45 +02:00
parent 58dd927afb
commit 0f869f3cf2
2 changed files with 28 additions and 4 deletions

View File

@ -161,6 +161,16 @@ public interface Vec {
@Contract(pure = true)
double z();
/**
* Gets the magnitude of the vector squared.
*
* @return the magnitude
*/
@Contract(pure = true)
default double lengthSquared() {
return MathUtils.square(x()) + MathUtils.square(y()) + MathUtils.square(z());
}
/**
* Gets the magnitude of the vector, defined as sqrt(x^2+y^2+z^2). The
* value of this method is not cached and uses a costly square-root
@ -170,8 +180,9 @@ public interface Vec {
*
* @return the magnitude
*/
@Contract(pure = true)
default double length() {
return Math.sqrt(MathUtils.square(x()) + MathUtils.square(y()) + MathUtils.square(z()));
return Math.sqrt(lengthSquared());
}
/**
@ -179,6 +190,7 @@ public interface Vec {
*
* @return the same vector
*/
@Contract(pure = true)
default @NotNull Vec normalize() {
final double length = length();
return with(x() / length, y() / length, z() / length);
@ -194,6 +206,7 @@ public interface Vec {
* @param vec the other vector
* @return the distance
*/
@Contract(pure = true)
default double distance(@NotNull Vec vec) {
return Math.sqrt(MathUtils.square(x() - vec.x()) +
MathUtils.square(y() - vec.y()) +
@ -206,6 +219,7 @@ public interface Vec {
* @param vec the other vector
* @return the squared distance
*/
@Contract(pure = true)
default double distanceSquared(@NotNull Vec vec) {
return MathUtils.square(x() - vec.x()) +
MathUtils.square(y() - vec.y()) +
@ -218,6 +232,7 @@ public interface Vec {
* @param vec the other vector
* @return angle in radians
*/
@Contract(pure = true)
default double angle(@NotNull Vec vec) {
final double dot = MathUtils.clamp(dot(vec) / (length() * vec.length()), -1.0, 1.0);
return Math.acos(dot);
@ -230,6 +245,7 @@ public interface Vec {
* @param vec the other vector
* @return dot product
*/
@Contract(pure = true)
default double dot(@NotNull Vec vec) {
return x() * vec.x() + y() * vec.y() + z() * vec.z();
}
@ -246,6 +262,7 @@ public interface Vec {
* @param o the other vector
* @return the same vector
*/
@Contract(pure = true)
default @NotNull Vec cross(@NotNull Vec o) {
return with(y() * o.z() - o.y() * z(),
z() * o.x() - o.z() * x(),
@ -260,6 +277,7 @@ public interface Vec {
* @param alpha The alpha value, must be between 0.0 and 1.0
* @return Linear interpolated vector
*/
@Contract(pure = true)
default @NotNull Vec lerp(@NotNull Vec vec, double alpha) {
final double x = x();
final double y = y();
@ -269,10 +287,16 @@ public interface Vec {
z + (alpha * (vec.z() - z)));
}
@Contract(pure = true)
default @NotNull Vec interpolate(@NotNull Vec target, double alpha, @NotNull Interpolation interpolation) {
return lerp(target, interpolation.apply(alpha));
}
@Contract(pure = true)
default boolean eq(@NotNull Vec vec) {
return VecImpl.equals(this, vec);
}
@FunctionalInterface
interface Operator {
@NotNull Vec apply(double x, double y, double z);

View File

@ -137,7 +137,7 @@ final class VecImpl {
}
}
private static boolean equals(@NotNull Vec vec1, Object o) {
static boolean equals(@NotNull Vec vec1, Object o) {
if (vec1 == o) return true;
if (!(o instanceof Vec)) return false;
Vec vec2 = (Vec) o;
@ -146,11 +146,11 @@ final class VecImpl {
Double.compare(vec1.z(), vec2.z()) == 0;
}
private static int hashCode(@NotNull Vec vec) {
static int hashCode(@NotNull Vec vec) {
return Objects.hash(vec.x(), vec.y(), vec.z());
}
private static @NotNull String toString(@NotNull Vec vec) {
static @NotNull String toString(@NotNull Vec vec) {
return "Vec3{" +
"x=" + vec.x() +
", y=" + vec.y() +