GlEngine/src/gl_engine/vec/Vec3i.java

143 lines
2.7 KiB
Java
Executable File

package gl_engine.vec;
import bdf.types.BdfObject;
import gl_engine.MathHelpers;
import gl_engine.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 Vec3i(Vec2i vec, int z) {
this(vec.x, vec.y, z);
}
public Vec3i(int x, Vec2i vec) {
this(x, vec.x, vec.y);
}
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 x = MathHelpers.mod(this.x, range.mx);
int y = MathHelpers.mod(this.y, range.my);
int z = MathHelpers.mod(this.z, range.mz);
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 add(int v) {
return new Vec3i(this.x + v, this.y + v, this.z + v);
}
public Vec3i subtract(int v) {
return new Vec3i(this.x - v, this.y - v, this.z - v);
}
public Vec3i multiply(int v) {
return new Vec3i(this.x * v, this.y * v, this.z * v);
}
public Vec3i divide(int v) {
return new Vec3i(this.x / v, this.y / v, this.z / v);
}
public Vec3i copy() {
return new Vec3i(x, y, z);
}
public int squareDistance(Vec3i other) {
return Math.abs(other.x - x) + Math.abs(other.y - y) + Math.abs(other.z - z);
}
public Vec3d toDouble() {
return new Vec3d(x, y, z);
}
public void BdfClassLoad(BdfObject bdf) {
int data[] = bdf.getIntegerArray();
if(data.length != 3) return;
x = data[0];
y = data[1];
z = data[2];
}
public void BdfClassSave(BdfObject bdf) {
bdf.setIntegerArray(new int[] {x, y, z});
};
public Vec3i(BdfObject bdf) {
BdfClassLoad(bdf);
}
public Vec2i xy() {
return new Vec2i(x, y);
}
public Vec2i xz() {
return new Vec2i(x, z);
}
public Vec2i yz() {
return new Vec2i(y, z);
}
}