|
| 1 | +package dev.felnull.fnjl.math; |
| 2 | + |
| 3 | +import dev.felnull.fnjl.tuple.FNQuadruple; |
| 4 | +import dev.felnull.fnjl.util.FNMath; |
| 5 | +import org.jetbrains.annotations.NotNull; |
| 6 | + |
| 7 | +import java.util.Objects; |
| 8 | + |
| 9 | +public class FNVec3f { |
| 10 | + private float x; |
| 11 | + private float y; |
| 12 | + private float z; |
| 13 | + |
| 14 | + public FNVec3f(float x, float y, float z) { |
| 15 | + this.x = x; |
| 16 | + this.y = y; |
| 17 | + this.z = z; |
| 18 | + } |
| 19 | + |
| 20 | + public float getX() { |
| 21 | + return x; |
| 22 | + } |
| 23 | + |
| 24 | + public float getY() { |
| 25 | + return y; |
| 26 | + } |
| 27 | + |
| 28 | + public float getZ() { |
| 29 | + return z; |
| 30 | + } |
| 31 | + |
| 32 | + public double distance(@NotNull FNVec3f vec) { |
| 33 | + return Math.sqrt(Math.pow(x - vec.getX(), 2) + Math.pow(y - vec.getY(), 2) + Math.pow(z - vec.getZ(), 2)); |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + public FNVec3f add(@NotNull FNVec3f vec) { |
| 38 | + this.x += vec.getX(); |
| 39 | + this.y += vec.getY(); |
| 40 | + this.z += vec.getZ(); |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + public FNVec3f sub(@NotNull FNVec3f vec) { |
| 45 | + this.x -= vec.getX(); |
| 46 | + this.y -= vec.getY(); |
| 47 | + this.z -= vec.getZ(); |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + public FNVec3f radians() { |
| 52 | + this.x = (float) Math.toRadians(x); |
| 53 | + this.y = (float) Math.toRadians(y); |
| 54 | + this.z = (float) Math.toRadians(z); |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + public FNVec3f degrees() { |
| 59 | + this.x = (float) Math.toDegrees(x); |
| 60 | + this.y = (float) Math.toDegrees(y); |
| 61 | + this.z = (float) Math.toDegrees(z); |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean equals(Object o) { |
| 67 | + if (this == o) return true; |
| 68 | + if (o == null || getClass() != o.getClass()) return false; |
| 69 | + FNVec3f fnVec3f = (FNVec3f) o; |
| 70 | + return Float.compare(fnVec3f.x, x) == 0 && Float.compare(fnVec3f.y, y) == 0 && Float.compare(fnVec3f.z, z) == 0; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public int hashCode() { |
| 75 | + return Objects.hash(x, y, z); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public String toString() { |
| 80 | + return "FNVec3f{" + |
| 81 | + "x=" + x + |
| 82 | + ", y=" + y + |
| 83 | + ", z=" + z + |
| 84 | + '}'; |
| 85 | + } |
| 86 | + |
| 87 | + public FNVec3d convertDouble() { |
| 88 | + return new FNVec3d(x, y, z); |
| 89 | + } |
| 90 | + |
| 91 | + public FNVec3i convertInt() { |
| 92 | + return new FNVec3i((int) x, (int) y, (int) z); |
| 93 | + } |
| 94 | + |
| 95 | + public FNVec4f toQuaternion() { |
| 96 | + FNQuadruple<Double, Double, Double, Double> q = FNMath.toQuaternion(x, y, z); |
| 97 | + double qx = q.getLeft(); |
| 98 | + double qy = q.getLeftCenter(); |
| 99 | + double qz = q.getRightCenter(); |
| 100 | + double qw = q.getRight(); |
| 101 | + return new FNVec4f((float) qx, (float) qy, (float) qz, (float) qw); |
| 102 | + } |
| 103 | +} |
0 commit comments