Refactor Vec3i

This commit is contained in:
basdxz committed 2022-05-31 13:17:29 +01:00
1 parent 779d5f635b
commit 7daab902a7
1 file changed
+69 -83
@@ -1,121 +1,107 @@
package com.falsepattern.lib.compat;
import com.falsepattern.lib.StableAPI;
import lombok.*;
import net.minecraft.util.MathHelper;
import javax.annotation.concurrent.Immutable;
import java.util.Objects;
/**
* A functional equivalent to Vec3i present in Minecraft 1.12
*/
@Getter
@Immutable
@EqualsAndHashCode
@AllArgsConstructor
@StableAPI(since = "0.6.0")
public class Vec3i implements Comparable<Vec3i> {
/**
* An immutable vector with zero as all coordinates.
* A static zero vector
*/
public static final Vec3i NULL_VECTOR = new Vec3i(0, 0, 0);
/**
* X coordinate
*/
private final int x;
/**
* Y coordinate
*/
private final int y;
/**
* Z coordinate
*/
private final int z;
public Vec3i(int xIn, int yIn, int zIn) {
this.x = xIn;
this.y = yIn;
this.z = zIn;
protected final int x;
protected final int y;
protected final int z;
/**
* Instantiates a new vector.
*
* @param x the x
* @param y the y
* @param z the z
*/
public Vec3i(double x, double y, double z) {
this(MathHelper.floor_double(x), MathHelper.floor_double(y), MathHelper.floor_double(z));
}
public Vec3i(double xIn, double yIn, double zIn) {
this(MathHelper.floor_double(xIn), MathHelper.floor_double(yIn), MathHelper.floor_double(zIn));
}
public int compareTo(Vec3i p_compareTo_1_) {
if (this.getY() == p_compareTo_1_.getY()) {
return this.getZ() == p_compareTo_1_.getZ() ? this.getX() - p_compareTo_1_.getX() : this.getZ() - p_compareTo_1_.getZ();
} else {
return this.getY() - p_compareTo_1_.getY();
}
public int compareTo(@NonNull Vec3i vec) {
return y == vec.getY() ? z == vec.getZ() ? x - vec.getX() : z - vec.getZ() : y - vec.getY();
}
/**
* Gets the X coordinate.
* Cross product between two vectors.
*
* @param vec the other vector
* @return the new resulting vector
*/
public int getX() {
return this.x;
public Vec3i crossProduct(@NonNull Vec3i vec) {
return new Vec3i(
y * vec.getZ() - z * vec.getY(),
z * vec.getX() - x * vec.getZ(),
x * vec.getY() - y * vec.getX()
);
}
/**
* Gets the Y coordinate.
* Gets distance to the vector.
*
* @param x the other x
* @param y the other y
* @param z the other z
* @return the distance
*/
public int getY() {
return this.y;
public double getDistance(int x, int y, int z) {
return Math.sqrt(distanceSq(x, y, z));
}
/**
* Gets the Z coordinate.
* Square distance between vectors.
*
* @param vec the other vector
* @return the square distance
*/
public int getZ() {
return this.z;
public double distanceSq(@NonNull Vec3i vec) {
return distanceSq(vec.getX(), vec.getY(), vec.getZ());
}
/**
* Calculate the cross product of this and the given Vector
* Square root distance to a point.
*
* @param x the other x
* @param y the other y
* @param z the other z
* @return the square distance
*/
public Vec3i crossProduct(Vec3i vec) {
return new Vec3i(this.getY() * vec.getZ() - this.getZ() * vec.getY(), this.getZ() * vec.getX() - this.getX() * vec.getZ(), this.getX() * vec.getY() - this.getY() * vec.getX());
}
public double getDistance(int xIn, int yIn, int zIn) {
double d0 = (this.getX() - xIn);
double d1 = (this.getY() - yIn);
double d2 = (this.getZ() - zIn);
return Math.sqrt(d0 * d0 + d1 * d1 + d2 * d2);
public double distanceSq(int x, int y, int z) {
val dX = this.x - x;
val dY = this.y - y;
val dZ = this.z - z;
return dX * dX + dY * dY + dZ * dZ;
}
/**
* Calculate squared distance to the given coordinates
* Square distance between point to center of this Block.
*
* @param x the other x
* @param y the other y
* @param z the other z
* @return the square distance to center
*/
public double distanceSq(double toX, double toY, double toZ) {
double d0 = (double) this.getX() - toX;
double d1 = (double) this.getY() - toY;
double d2 = (double) this.getZ() - toZ;
return d0 * d0 + d1 * d1 + d2 * d2;
}
/**
* Compute square of distance from point x, y, z to center of this Block
*/
public double distanceSqToCenter(double xIn, double yIn, double zIn) {
double d0 = (double) this.getX() + 0.5D - xIn;
double d1 = (double) this.getY() + 0.5D - yIn;
double d2 = (double) this.getZ() + 0.5D - zIn;
return d0 * d0 + d1 * d1 + d2 * d2;
}
/**
* Calculate squared distance to the given Vector
*/
public double distanceSq(Vec3i to) {
return this.distanceSq(to.getX(), to.getY(), to.getZ());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Vec3i vec3i = (Vec3i) o;
return getX() == vec3i.getX() && getY() == vec3i.getY() && getZ() == vec3i.getZ();
}
@Override
public int hashCode() {
return Objects.hash(getX(), getY(), getZ());
public double distanceSqToCenter(double x, double y, double z) {
val dX = this.x + 0.5D - x;
val dY = this.y + 0.5D - y;
val dZ = this.z + 0.5D - z;
return dX * dX + dY * dY + dZ * dZ;
}
}