make vector2 immutable by default

This commit is contained in:
dfsek
2021-12-20 00:19:09 -07:00
parent 026547bdfc
commit 961a42d1cb
14 changed files with 164 additions and 136 deletions

View File

@@ -16,10 +16,10 @@ import com.dfsek.terra.api.block.state.properties.enums.RailShape;
import com.dfsek.terra.api.block.state.properties.enums.RedstoneConnection;
import com.dfsek.terra.api.block.state.properties.enums.WallHeight;
import com.dfsek.terra.api.util.vector.Vector2;
import com.dfsek.terra.api.util.vector.Vector2.Mutable;
public final class RotationUtil {
/**
* Rotate and mirror a coordinate pair.
*
@@ -29,15 +29,13 @@ public final class RotationUtil {
* @return Rotated vector
*/
public static Vector2 rotateVector(Vector2 orig, Rotation r) {
Vector2 copy = orig.clone();
Mutable copy = orig.mutable();
switch(r) {
case CW_90 -> copy.setX(orig.getZ()).setZ(-orig.getX());
case CCW_90 -> copy.setX(-orig.getZ()).setZ(orig.getX());
case CW_180 -> copy.multiply(-1);
}
orig.setX(copy.getX());
orig.setZ(copy.getZ());
return orig;
return copy.immutable();
}

View File

@@ -16,8 +16,8 @@ import com.dfsek.terra.api.util.MathUtil;
* oh yeah
*/
public class Vector2 implements Cloneable {
private double x;
private double z;
private final double x;
private final double z;
/**
* Create a vector with a given X and Z component
@@ -34,67 +34,7 @@ public class Vector2 implements Cloneable {
return new Vector2(x, z);
}
/**
* Multiply X and Z components by a value.
*
* @param m Value to multiply
*
* @return Mutated vector, for chaining.
*/
public Vector2 multiply(double m) {
x *= m;
z *= m;
return this;
}
/**
* Add this vector to another.
*
* @param other Vector to add
*
* @return Mutated vector, for chaining.
*/
public Vector2 add(Vector2 other) {
x += other.getX();
z += other.getZ();
return this;
}
/**
* Subtract a vector from this vector,
*
* @param other Vector to subtract
*
* @return Mutated vector, for chaining.
*/
public Vector2 subtract(Vector2 other) {
x -= other.getX();
z -= other.getZ();
return this;
}
/**
* Normalize this vector to length 1
*
* @return Mutated vector, for chaining.
*/
public Vector2 normalize() {
divide(length());
return this;
}
/**
* Divide X and Z components by a value.
*
* @param d Divisor
*
* @return Mutated vector, for chaining.
*/
public Vector2 divide(double d) {
x /= d;
z /= d;
return this;
}
/**
* Get the length of this Vector
@@ -142,12 +82,6 @@ public class Vector2 implements Cloneable {
return Vector3.of(this.x, y, this.z);
}
public Vector2 add(double x, double z) {
this.x += x;
this.z += z;
return this;
}
/**
* Get X component
*
@@ -157,10 +91,6 @@ public class Vector2 implements Cloneable {
return x;
}
public Vector2 setX(double x) {
this.x = x;
return this;
}
/**
* Get Z component
@@ -171,10 +101,6 @@ public class Vector2 implements Cloneable {
return z;
}
public Vector2 setZ(double z) {
this.z = z;
return this;
}
public int getBlockX() {
return FastMath.floorToInt(x);
@@ -197,11 +123,124 @@ public class Vector2 implements Cloneable {
return MathUtil.equals(this.x, other.x) && MathUtil.equals(this.z, other.z);
}
public Vector2 clone() {
try {
return (Vector2) super.clone();
} catch(CloneNotSupportedException e) {
throw new Error(e);
public Mutable mutable() {
return new Mutable(x, z);
}
public static class Mutable {
private double x, z;
private Mutable(double x, double z) {
this.x = x;
this.z = z;
}
public double getX() {
return x;
}
public double getZ() {
return z;
}
public Vector2 immutable() {
return Vector2.of(x, z);
}
public Mutable setX(double x) {
this.x = x;
return this;
}
public Mutable setZ(double z) {
this.z = z;
return this;
}
/**
* Get the length of this Vector
*
* @return length
*/
public double length() {
return FastMath.sqrt(lengthSquared());
}
/**
* Get the squared length of this Vector
*
* @return squared length
*/
public double lengthSquared() {
return x * x + z * z;
}
public Mutable add(double x, double z) {
this.x += x;
this.z += z;
return this;
}
/**
* Multiply X and Z components by a value.
*
* @param m Value to multiply
*
* @return Mutated vector, for chaining.
*/
public Mutable multiply(double m) {
x *= m;
z *= m;
return this;
}
/**
* Add this vector to another.
*
* @param other Vector to add
*
* @return Mutated vector, for chaining.
*/
public Mutable add(Vector2 other) {
x += other.getX();
z += other.getZ();
return this;
}
/**
* Subtract a vector from this vector,
*
* @param other Vector to subtract
*
* @return Mutated vector, for chaining.
*/
public Mutable subtract(Vector2 other) {
x -= other.getX();
z -= other.getZ();
return this;
}
/**
* Normalize this vector to length 1
*
* @return Mutated vector, for chaining.
*/
public Mutable normalize() {
divide(length());
return this;
}
/**
* Divide X and Z components by a value.
*
* @param d Divisor
*
* @return Mutated vector, for chaining.
*/
public Mutable divide(double d) {
x /= d;
z /= d;
return this;
}
}