From 0f3342b36875a83631a3c5908d57a5baf05585e4 Mon Sep 17 00:00:00 2001 From: iTitus Date: Tue, 25 May 2021 17:04:22 +0200 Subject: [PATCH] Add more vector types --- .../github/ititus/math/number/BigComplex.java | 2 +- .../github/ititus/math/quaternion/QuatD.java | 61 +++++++++++++++ .../io/github/ititus/math/vector/Vec2d.java | 57 ++++++++++++++ .../io/github/ititus/math/vector/Vec2f.java | 57 ++++++++++++++ .../io/github/ititus/math/vector/Vec2i.java | 19 +++++ .../io/github/ititus/math/vector/Vec3d.java | 64 ++++++++++++++++ .../io/github/ititus/math/vector/Vec3f.java | 10 +++ .../io/github/ititus/math/vector/Vec3i.java | 25 ++++++ .../io/github/ititus/math/vector/Vec4d.java | 76 +++++++++++++++++++ .../io/github/ititus/math/vector/Vec4f.java | 11 +++ .../io/github/ititus/math/vector/Vec4i.java | 31 ++++++++ 11 files changed, 412 insertions(+), 1 deletion(-) create mode 100644 src/main/java/io/github/ititus/math/quaternion/QuatD.java create mode 100644 src/main/java/io/github/ititus/math/vector/Vec2d.java create mode 100644 src/main/java/io/github/ititus/math/vector/Vec2f.java create mode 100644 src/main/java/io/github/ititus/math/vector/Vec3d.java create mode 100644 src/main/java/io/github/ititus/math/vector/Vec4d.java diff --git a/src/main/java/io/github/ititus/math/number/BigComplex.java b/src/main/java/io/github/ititus/math/number/BigComplex.java index 52cd94a..96ab1e3 100644 --- a/src/main/java/io/github/ititus/math/number/BigComplex.java +++ b/src/main/java/io/github/ititus/math/number/BigComplex.java @@ -229,7 +229,7 @@ public static BigComplex of(String s) { } public static BigComplex of(Vec2i v) { - return of(v.getX(), v.getY()); + return of(v.x(), v.y()); } public static BigComplex of(byte real, byte imag) { diff --git a/src/main/java/io/github/ititus/math/quaternion/QuatD.java b/src/main/java/io/github/ititus/math/quaternion/QuatD.java new file mode 100644 index 0000000..6e1f38d --- /dev/null +++ b/src/main/java/io/github/ititus/math/quaternion/QuatD.java @@ -0,0 +1,61 @@ +package io.github.ititus.math.quaternion; + +import io.github.ititus.data.ArrayUtil; +import io.github.ititus.math.vector.Vec4d; + +public final class QuatD { + + private final double x; + private final double y; + private final double z; + private final double w; + + public QuatD(Vec4d vec4d) { + this(vec4d.x(), vec4d.y(), vec4d.z(), vec4d.w()); + } + + public QuatD(double x, double y, double z, double w) { + this.x = x; + this.y = y; + this.z = z; + this.w = w; + } + + public double x() { + return x; + } + + public double y() { + return y; + } + + public double z() { + return z; + } + + public double w() { + return w; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } else if (!(o instanceof QuatD)) { + return false; + } + + QuatD quatD = (QuatD) o; + return Double.compare(quatD.x, x) == 0 && Double.compare(quatD.y, y) == 0 && Double.compare(quatD.z, z) == 0 && Double.compare(quatD.w, w) == 0; + } + + @Override + public int hashCode() { + return ArrayUtil.hash(x, y, z, w); + } + + @Override + public String toString() { + return "(" + x + ", " + y + ", " + z + ", " + w + ')'; + } +} diff --git a/src/main/java/io/github/ititus/math/vector/Vec2d.java b/src/main/java/io/github/ititus/math/vector/Vec2d.java new file mode 100644 index 0000000..e448daa --- /dev/null +++ b/src/main/java/io/github/ititus/math/vector/Vec2d.java @@ -0,0 +1,57 @@ +package io.github.ititus.math.vector; + +import io.github.ititus.data.ArrayUtil; + +public final class Vec2d { + + private final double x; + private final double y; + + public Vec2d() { + this(0, 0); + } + + public Vec2d(double... arr) { + if (arr.length != 2) { + throw new IllegalArgumentException("illegal array size"); + } + + this.x = arr[0]; + this.y = arr[1]; + } + + public Vec2d(double x, double y) { + this.x = x; + this.y = y; + } + + public double x() { + return x; + } + + public double y() { + return y; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } else if (!(o instanceof Vec2d)) { + return false; + } + + Vec2d vec2d = (Vec2d) o; + return Double.compare(vec2d.x, x) == 0 && Double.compare(vec2d.y, y) == 0; + } + + @Override + public int hashCode() { + return ArrayUtil.hash(x, y); + } + + @Override + public String toString() { + return "(" + x + ", " + y + ')'; + } +} diff --git a/src/main/java/io/github/ititus/math/vector/Vec2f.java b/src/main/java/io/github/ititus/math/vector/Vec2f.java new file mode 100644 index 0000000..aafa21e --- /dev/null +++ b/src/main/java/io/github/ititus/math/vector/Vec2f.java @@ -0,0 +1,57 @@ +package io.github.ititus.math.vector; + +import io.github.ititus.data.ArrayUtil; + +public final class Vec2f { + + private final float x; + private final float y; + + public Vec2f() { + this(0, 0); + } + + public Vec2f(float... arr) { + if (arr.length != 2) { + throw new IllegalArgumentException("illegal array size"); + } + + this.x = arr[0]; + this.y = arr[1]; + } + + public Vec2f(float x, float y) { + this.x = x; + this.y = y; + } + + public float x() { + return x; + } + + public float y() { + return y; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } else if (!(o instanceof Vec2f)) { + return false; + } + + Vec2f vec2f = (Vec2f) o; + return Float.compare(vec2f.x, x) == 0 && Float.compare(vec2f.y, y) == 0; + } + + @Override + public int hashCode() { + return ArrayUtil.hash(x, y); + } + + @Override + public String toString() { + return "(" + x + ", " + y + ')'; + } +} diff --git a/src/main/java/io/github/ititus/math/vector/Vec2i.java b/src/main/java/io/github/ititus/math/vector/Vec2i.java index 1c6b79d..12a98dd 100644 --- a/src/main/java/io/github/ititus/math/vector/Vec2i.java +++ b/src/main/java/io/github/ititus/math/vector/Vec2i.java @@ -11,6 +11,15 @@ public Vec2i() { this(0, 0); } + public Vec2i(int... arr) { + if (arr.length != 2) { + throw new IllegalArgumentException("illegal array size"); + } + + this.x = arr[0]; + this.y = arr[1]; + } + public Vec2i(int x, int y) { this.x = x; this.y = y; @@ -162,14 +171,24 @@ public Vec2i rotateCCW(int degrees) { } } + @Deprecated(forRemoval = true) public int getX() { return x; } + public int x() { + return x; + } + + @Deprecated(forRemoval = true) public int getY() { return y; } + public int y() { + return y; + } + @Override public int compareTo(Vec2i o) { int c = Integer.compare(x, o.x); diff --git a/src/main/java/io/github/ititus/math/vector/Vec3d.java b/src/main/java/io/github/ititus/math/vector/Vec3d.java new file mode 100644 index 0000000..38fce8c --- /dev/null +++ b/src/main/java/io/github/ititus/math/vector/Vec3d.java @@ -0,0 +1,64 @@ +package io.github.ititus.math.vector; + +import io.github.ititus.data.ArrayUtil; + +public final class Vec3d { + + private final double x; + private final double y; + private final double z; + + public Vec3d() { + this(0, 0, 0); + } + + public Vec3d(double... arr) { + if (arr.length != 3) { + throw new IllegalArgumentException("illegal array size"); + } + + this.x = arr[0]; + this.y = arr[1]; + this.z = arr[2]; + } + + public Vec3d(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; + } + + public double x() { + return x; + } + + public double y() { + return y; + } + + public double z() { + return z; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } else if (!(o instanceof Vec3d)) { + return false; + } + + Vec3d vec3d = (Vec3d) o; + return Double.compare(vec3d.x, x) == 0 && Double.compare(vec3d.y, y) == 0 && Double.compare(vec3d.z, z) == 0; + } + + @Override + public int hashCode() { + return ArrayUtil.hash(x, y, z); + } + + @Override + public String toString() { + return "(" + x + ", " + y + ", " + z + ')'; + } +} diff --git a/src/main/java/io/github/ititus/math/vector/Vec3f.java b/src/main/java/io/github/ititus/math/vector/Vec3f.java index f7bc882..84fe400 100644 --- a/src/main/java/io/github/ititus/math/vector/Vec3f.java +++ b/src/main/java/io/github/ititus/math/vector/Vec3f.java @@ -12,6 +12,16 @@ public Vec3f() { this(0, 0, 0); } + public Vec3f(float... arr) { + if (arr.length != 3) { + throw new IllegalArgumentException("illegal array size"); + } + + this.x = arr[0]; + this.y = arr[1]; + this.z = arr[2]; + } + public Vec3f(float x, float y, float z) { this.x = x; this.y = y; diff --git a/src/main/java/io/github/ititus/math/vector/Vec3i.java b/src/main/java/io/github/ititus/math/vector/Vec3i.java index c71cd66..7015ee0 100644 --- a/src/main/java/io/github/ititus/math/vector/Vec3i.java +++ b/src/main/java/io/github/ititus/math/vector/Vec3i.java @@ -13,6 +13,16 @@ public Vec3i() { this(0, 0, 0); } + public Vec3i(int... arr) { + if (arr.length != 3) { + throw new IllegalArgumentException("illegal array size"); + } + + this.x = arr[0]; + this.y = arr[1]; + this.z = arr[2]; + } + public Vec3i(int x, int y, int z) { this.x = x; this.y = y; @@ -35,18 +45,33 @@ public int manhattanDistance() { return Math.abs(x) + Math.abs(y) + Math.abs(z); } + @Deprecated(forRemoval = true) public int getX() { return x; } + public int x() { + return x; + } + + @Deprecated(forRemoval = true) public int getY() { return y; } + public int y() { + return y; + } + + @Deprecated(forRemoval = true) public int getZ() { return z; } + public int z() { + return z; + } + @Override public int compareTo(Vec3i o) { int c1 = Integer.compare(x, o.x); diff --git a/src/main/java/io/github/ititus/math/vector/Vec4d.java b/src/main/java/io/github/ititus/math/vector/Vec4d.java new file mode 100644 index 0000000..857f994 --- /dev/null +++ b/src/main/java/io/github/ititus/math/vector/Vec4d.java @@ -0,0 +1,76 @@ +package io.github.ititus.math.vector; + +import io.github.ititus.data.ArrayUtil; +import io.github.ititus.math.quaternion.QuatD; + +public final class Vec4d { + + private final double x; + private final double y; + private final double z; + private final double w; + + public Vec4d() { + this(0, 0, 0, 0); + } + + public Vec4d(QuatD quatD) { + this(quatD.x(), quatD.y(), quatD.z(), quatD.w()); + } + + public Vec4d(double... arr) { + if (arr.length != 4) { + throw new IllegalArgumentException("illegal array size"); + } + + this.x = arr[0]; + this.y = arr[1]; + this.z = arr[2]; + this.w = arr[3]; + } + + public Vec4d(double x, double y, double z, double w) { + this.x = x; + this.y = y; + this.z = z; + this.w = w; + } + + public double x() { + return x; + } + + public double y() { + return y; + } + + public double z() { + return z; + } + + public double w() { + return w; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } else if (!(o instanceof Vec4d)) { + return false; + } + + Vec4d vec4d = (Vec4d) o; + return Double.compare(vec4d.x, x) == 0 && Double.compare(vec4d.y, y) == 0 && Double.compare(vec4d.z, z) == 0 && Double.compare(vec4d.w, w) == 0; + } + + @Override + public int hashCode() { + return ArrayUtil.hash(x, y, z, w); + } + + @Override + public String toString() { + return "(" + x + ", " + y + ", " + z + ", " + w + ')'; + } +} diff --git a/src/main/java/io/github/ititus/math/vector/Vec4f.java b/src/main/java/io/github/ititus/math/vector/Vec4f.java index 3f25668..9d25396 100644 --- a/src/main/java/io/github/ititus/math/vector/Vec4f.java +++ b/src/main/java/io/github/ititus/math/vector/Vec4f.java @@ -18,6 +18,17 @@ public Vec4f(QuatF quatF) { this(quatF.x(), quatF.y(), quatF.z(), quatF.w()); } + public Vec4f(float... arr) { + if (arr.length != 4) { + throw new IllegalArgumentException("illegal array size"); + } + + this.x = arr[0]; + this.y = arr[1]; + this.z = arr[2]; + this.w = arr[3]; + } + public Vec4f(float x, float y, float z, float w) { this.x = x; this.y = y; diff --git a/src/main/java/io/github/ititus/math/vector/Vec4i.java b/src/main/java/io/github/ititus/math/vector/Vec4i.java index 8bc88f6..b0011fb 100644 --- a/src/main/java/io/github/ititus/math/vector/Vec4i.java +++ b/src/main/java/io/github/ititus/math/vector/Vec4i.java @@ -14,6 +14,17 @@ public Vec4i() { this(0, 0, 0, 0); } + public Vec4i(int... arr) { + if (arr.length != 4) { + throw new IllegalArgumentException("illegal array size"); + } + + this.x = arr[0]; + this.y = arr[1]; + this.z = arr[2]; + this.w = arr[3]; + } + public Vec4i(int x, int y, int z, int w) { this.x = x; this.y = y; @@ -37,22 +48,42 @@ public int manhattanDistance() { return Math.abs(x) + Math.abs(y) + Math.abs(z) + Math.abs(w); } + @Deprecated(forRemoval = true) public int getX() { return x; } + public int x() { + return x; + } + + @Deprecated(forRemoval = true) public int getY() { return y; } + public int y() { + return y; + } + + @Deprecated(forRemoval = true) public int getZ() { return z; } + public int z() { + return z; + } + + @Deprecated(forRemoval = true) public int getW() { return w; } + public int w() { + return w; + } + @Override public int compareTo(Vec4i o) { int c1 = Integer.compare(x, o.x);