package maze_game.helpers.vec;

import maze_game.helpers.MathHelpers;
import maze_game.helpers.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 x = MathHelpers.mod(this.x, range.mx);
		int y = MathHelpers.mod(this.y, range.my);
		
		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 = MathHelpers.mod(id, range.mx);
		id -= x;
		id /= range.mx;
		int y = MathHelpers.mod(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);
	}
	
	public Vec2i add(int v) {
		return new Vec2i(this.x + v, this.y + v);
	}
	
	public Vec2i subtract(int v) {
		return new Vec2i(this.x - v, this.y - v);
	}
	
	public Vec2i multiply(int v) {
		return new Vec2i(this.x * v, this.y * v);
	}
	
	public Vec2i divide(int v) {
		return new Vec2i(this.x / v, this.y / v);
	}
	
	public Vec2i copy() {
		return new Vec2i(x, y);
	}
	
	public double squareDistance(Vec2i other)
	{
		int dx = MathHelpers.positive(other.x - x);
		int dy = MathHelpers.positive(other.y - y);
		return MathHelpers.biggest(dx, dy);
	}
	
	public Vec2d toDouble() {
		return new Vec2d(x, y);
	}
}