2022-12-09 23:00:14 +01:00
|
|
|
package universalelectricity.core.vector;
|
|
|
|
|
|
|
|
import net.minecraft.util.MathHelper;
|
2023-07-16 12:31:56 +02:00
|
|
|
import universalelectricity.api.vector.IVector2;
|
2022-12-09 23:00:14 +01:00
|
|
|
|
2023-07-16 12:31:56 +02:00
|
|
|
public class Vector2 implements Cloneable, IVector2 {
|
2022-12-09 23:00:14 +01:00
|
|
|
|
|
|
|
public double x;
|
|
|
|
public double y;
|
|
|
|
|
|
|
|
|
|
|
|
public Vector2() {
|
2023-07-16 12:31:56 +02:00
|
|
|
this(0.0D, 0.0D);
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2(double x, double y) {
|
2023-07-16 12:31:56 +02:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int intX() {
|
2023-07-16 12:31:56 +02:00
|
|
|
return (int)Math.floor(this.x);
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int intY() {
|
2023-07-16 12:31:56 +02:00
|
|
|
return (int)Math.floor(this.y);
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 clone() {
|
2023-07-16 12:31:56 +02:00
|
|
|
return new Vector2(this.x, this.y);
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static double distance(Vector2 point1, Vector2 point2) {
|
2023-07-16 12:31:56 +02:00
|
|
|
double xDifference = point1.x - point2.x;
|
|
|
|
double yDiference = point1.y - point2.y;
|
|
|
|
return (double)MathHelper.sqrt_double(xDifference * xDifference + yDiference * yDiference);
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static double slope(Vector2 point1, Vector2 point2) {
|
2023-07-16 12:31:56 +02:00
|
|
|
double xDifference = point1.x - point2.x;
|
|
|
|
double yDiference = point1.y - point2.y;
|
|
|
|
return yDiference / xDifference;
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public double distanceTo(Vector2 target) {
|
2023-07-16 12:31:56 +02:00
|
|
|
double xDifference = this.x - target.x;
|
|
|
|
double yDifference = this.y - target.y;
|
|
|
|
return (double)MathHelper.sqrt_double(xDifference * xDifference + yDifference * yDifference);
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 add(Vector2 par1) {
|
2023-07-16 12:31:56 +02:00
|
|
|
this.x += par1.x;
|
|
|
|
this.y += par1.y;
|
|
|
|
return this;
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 add(double par1) {
|
2023-07-16 12:31:56 +02:00
|
|
|
this.x += par1;
|
|
|
|
this.y += par1;
|
|
|
|
return this;
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 invert() {
|
2023-07-16 12:31:56 +02:00
|
|
|
this.multiply(-1.0D);
|
|
|
|
return this;
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 multiply(double amount) {
|
2023-07-16 12:31:56 +02:00
|
|
|
this.x *= amount;
|
|
|
|
this.y *= amount;
|
|
|
|
return this;
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 round() {
|
2023-07-16 12:31:56 +02:00
|
|
|
return new Vector2((double)Math.round(this.x), (double)Math.round(this.y));
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 ceil() {
|
2023-07-16 12:31:56 +02:00
|
|
|
return new Vector2(Math.ceil(this.x), Math.ceil(this.y));
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 floor() {
|
2023-07-16 12:31:56 +02:00
|
|
|
return new Vector2(Math.floor(this.x), Math.floor(this.y));
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int hashCode() {
|
2023-07-16 12:31:56 +02:00
|
|
|
return ("X:" + this.x + "Y:" + this.y).hashCode();
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean equals(Object o) {
|
2023-07-16 12:31:56 +02:00
|
|
|
if(!(o instanceof Vector2)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
Vector2 vector = (Vector2)o;
|
|
|
|
return this.x == vector.x && this.y == vector.y;
|
|
|
|
}
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String toString() {
|
2023-07-16 12:31:56 +02:00
|
|
|
return "Vector2 [" + this.x + "," + this.y + "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double x() {
|
|
|
|
return this.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public double y() {
|
|
|
|
return this.y;
|
2022-12-09 23:00:14 +01:00
|
|
|
}
|
|
|
|
}
|