Skip to content

Commit

Permalink
Add more vector types
Browse files Browse the repository at this point in the history
  • Loading branch information
iTitus committed May 25, 2021
1 parent 119ec86 commit 0f3342b
Show file tree
Hide file tree
Showing 11 changed files with 412 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/io/github/ititus/math/number/BigComplex.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/io/github/ititus/math/quaternion/QuatD.java
Original file line number Diff line number Diff line change
@@ -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 + ')';
}
}
57 changes: 57 additions & 0 deletions src/main/java/io/github/ititus/math/vector/Vec2d.java
Original file line number Diff line number Diff line change
@@ -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 + ')';
}
}
57 changes: 57 additions & 0 deletions src/main/java/io/github/ititus/math/vector/Vec2f.java
Original file line number Diff line number Diff line change
@@ -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 + ')';
}
}
19 changes: 19 additions & 0 deletions src/main/java/io/github/ititus/math/vector/Vec2i.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/io/github/ititus/math/vector/Vec3d.java
Original file line number Diff line number Diff line change
@@ -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 + ')';
}
}
10 changes: 10 additions & 0 deletions src/main/java/io/github/ititus/math/vector/Vec3f.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/io/github/ititus/math/vector/Vec3i.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 0f3342b

Please sign in to comment.