Files
FalsePatternLib/src/main/java/com/falsepattern/lib/compat/Vec3i.java
T

107 lines
2.7 KiB
Java
Raw Normal View History

2022-05-06 17:41:02 +02:00
package com.falsepattern.lib.compat;
2022-05-06 23:53:11 +02:00
import com.falsepattern.lib.StableAPI;
2022-05-31 12:36:56 +01:00
import lombok.*;
2022-05-06 17:41:02 +02:00
import net.minecraft.util.MathHelper;
import javax.annotation.concurrent.Immutable;
2022-05-31 12:36:56 +01:00
/**
* A functional equivalent to Vec3i present in Minecraft 1.12
*/
@Getter
2022-05-06 17:41:02 +02:00
@Immutable
2022-05-31 12:36:56 +01:00
@EqualsAndHashCode
@AllArgsConstructor
2022-05-06 23:53:11 +02:00
@StableAPI(since = "0.6.0")
2022-05-06 17:41:02 +02:00
public class Vec3i implements Comparable<Vec3i> {
/**
2022-05-31 12:36:56 +01:00
* A static zero vector
2022-05-06 17:41:02 +02:00
*/
public static final Vec3i NULL_VECTOR = new Vec3i(0, 0, 0);
2022-05-31 12:36:56 +01:00
protected final int x;
protected final int y;
protected final int z;
2022-05-06 17:41:02 +02:00
/**
2022-05-31 12:36:56 +01:00
* Instantiates a new vector.
*
* @param x the x
* @param y the y
* @param z the z
2022-05-06 17:41:02 +02:00
*/
2022-05-31 12:36:56 +01:00
public Vec3i(double x, double y, double z) {
this(MathHelper.floor_double(x), MathHelper.floor_double(y), MathHelper.floor_double(z));
2022-05-06 17:41:02 +02:00
}
2022-05-31 12:36:56 +01:00
public int compareTo(@NonNull Vec3i vec) {
return y == vec.getY() ? z == vec.getZ() ? x - vec.getX() : z - vec.getZ() : y - vec.getY();
2022-05-06 17:41:02 +02:00
}
/**
2022-05-31 12:36:56 +01:00
* Cross product between two vectors.
*
* @param vec the other vector
* @return the new resulting vector
2022-05-06 17:41:02 +02:00
*/
2022-05-31 12:36:56 +01:00
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()
);
2022-05-06 17:41:02 +02:00
}
/**
2022-05-31 12:36:56 +01:00
* Gets distance to the vector.
*
* @param x the other x
* @param y the other y
* @param z the other z
* @return the distance
2022-05-06 17:41:02 +02:00
*/
2022-05-31 12:36:56 +01:00
public double getDistance(int x, int y, int z) {
return Math.sqrt(distanceSq(x, y, z));
2022-05-06 17:41:02 +02:00
}
/**
2022-05-31 12:36:56 +01:00
* Square distance between vectors.
*
* @param vec the other vector
* @return the square distance
2022-05-06 17:41:02 +02:00
*/
2022-05-31 12:36:56 +01:00
public double distanceSq(@NonNull Vec3i vec) {
return distanceSq(vec.getX(), vec.getY(), vec.getZ());
2022-05-06 17:41:02 +02:00
}
/**
2022-05-31 12:36:56 +01:00
* 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
2022-05-06 17:41:02 +02:00
*/
2022-05-31 12:36:56 +01:00
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;
2022-05-06 17:41:02 +02:00
}
/**
2022-05-31 12:36:56 +01:00
* 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
2022-05-06 17:41:02 +02:00
*/
2022-05-31 12:36:56 +01:00
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;
2022-05-06 17:41:02 +02:00
}
}