67 lines
1.2 KiB
Java
67 lines
1.2 KiB
Java
package shootergame.util.math.vec;
|
|
|
|
import shootergame.util.math.MathHelpers;
|
|
import shootergame.util.math.range.Range2i;
|
|
|
|
public class Vec2i
|
|
{
|
|
public int x;
|
|
public int y;
|
|
|
|
public Vec2i(int x, int y)
|
|
{
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
|
|
public double distance(Vec2i other) {
|
|
return MathHelpers.distance2d(x, y, other.x, other.y);
|
|
}
|
|
|
|
public static double distance(Vec2i v1, Vec2i v2) {
|
|
return v1.distance(v2);
|
|
}
|
|
|
|
public int getId(Range2i range)
|
|
{
|
|
int id = 0;
|
|
int m = 1;
|
|
|
|
id += x;
|
|
m = range.mx;
|
|
id += y*m;
|
|
|
|
return id;
|
|
}
|
|
|
|
public static Vec2i fromId(Range2i range, int id)
|
|
{
|
|
int x = id % range.mx;
|
|
id -= x;
|
|
id /= range.mx;
|
|
int y = id % range.my;
|
|
|
|
return new Vec2i(x, y);
|
|
}
|
|
|
|
public boolean equal(Vec2i other) {
|
|
return x == other.x && y == other.y;
|
|
}
|
|
|
|
public Vec2i add(Vec2i other) {
|
|
return new Vec2i(this.x + other.x, this.y + other.y);
|
|
}
|
|
|
|
public Vec2i subtract(Vec2i other) {
|
|
return new Vec2i(this.x - other.x, this.y - other.y);
|
|
}
|
|
|
|
public Vec2i multiply(Vec2i other) {
|
|
return new Vec2i(this.x * other.x, this.y * other.y);
|
|
}
|
|
|
|
public Vec2i divide(Vec2i other) {
|
|
return new Vec2i(this.x / other.x, this.y / other.y);
|
|
}
|
|
}
|