89 lines
1.7 KiB
Java
89 lines
1.7 KiB
Java
package shootergame.util.math.vec;
|
|
|
|
import shootergame.util.math.MathHelpers;
|
|
import shootergame.util.math.range.Range3i;
|
|
|
|
public class Vec3i
|
|
{
|
|
public int x;
|
|
public int y;
|
|
public int z;
|
|
|
|
public Vec3i(int x, int y, int z)
|
|
{
|
|
this.x = x;
|
|
this.y = y;
|
|
this.z = z;
|
|
}
|
|
|
|
public double distance(Vec3i other) {
|
|
return MathHelpers.distance3d(x, y, z, other.x, other.y, other.z);
|
|
}
|
|
|
|
public static double distance(Vec3i v1, Vec3i v2) {
|
|
return v1.distance(v2);
|
|
}
|
|
|
|
public int getId(Range3i range)
|
|
{
|
|
int id = 0;
|
|
int m = 1;
|
|
|
|
id += x;
|
|
m = range.mx;
|
|
id += y*m;
|
|
m *= range.my;
|
|
id += z*m;
|
|
|
|
return id;
|
|
}
|
|
|
|
public static Vec3i fromId(Range3i range, int id)
|
|
{
|
|
int x = MathHelpers.mod(id, range.mx);
|
|
id -= x;
|
|
id /= range.mx;
|
|
int y = MathHelpers.mod(id, range.my);
|
|
id -= y;
|
|
id /= range.my;
|
|
int z = MathHelpers.mod(id, range.mz);
|
|
|
|
return new Vec3i(x, y, z);
|
|
}
|
|
|
|
public boolean equal(Vec3i other) {
|
|
return x == other.x && y == other.y && z == other.z;
|
|
}
|
|
|
|
public Vec3i add(Vec3i other) {
|
|
return new Vec3i(this.x + other.x, this.y + other.y, this.z + other.z);
|
|
}
|
|
|
|
public Vec3i subtract(Vec3i other) {
|
|
return new Vec3i(this.x - other.x, this.y - other.y, this.z - other.z);
|
|
}
|
|
|
|
public Vec3i multiply(Vec3i other) {
|
|
return new Vec3i(this.x * other.x, this.y * other.y, this.z * other.z);
|
|
}
|
|
|
|
public Vec3i divide(Vec3i other) {
|
|
return new Vec3i(this.x / other.x, this.y / other.y, this.z / other.z);
|
|
}
|
|
|
|
public Vec3i copy() {
|
|
return new Vec3i(x, y, z);
|
|
}
|
|
|
|
public int squareDistance(Vec3i other)
|
|
{
|
|
int dx = MathHelpers.positive(other.x - x);
|
|
int dy = MathHelpers.positive(other.y - y);
|
|
int dz = MathHelpers.positive(other.z - z);
|
|
|
|
if(dx > dy) return dx;
|
|
if(dy > dz) return dy;
|
|
else return dz;
|
|
}
|
|
}
|