chess-board/ChessPanel/Vector.cs

68 lines
1.9 KiB
C#
Raw Normal View History

2017-02-27 15:15:50 +01:00
namespace Chess
{
public class Vector
{
private int x;
private int y;
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
public Vector(int x, int y)
{
this.x = x;
this.y = y;
}
public static Vector operator +(Vector v1, Vector v2) => new Vector(v1.x + v2.x, v1.y + v2.y);
public static Vector operator -(Vector v1, Vector v2) => new Vector(v1.x - v2.x, v1.y - v2.y);
public static Vector operator /(Vector v, int n) => new Vector(v.x / n, v.y / n);
public static Vector operator *(Vector v, int n) => new Vector(v.x * n, v.y * n);
public static Vector operator -(Vector v) => new Vector(-v.x, -v.y);
public static Vector operator +(Vector v) => v;
public static bool operator ==(Vector v1, Vector v2)
{
if (v1 as object == null || v2 as object == null)
{
return false;
}
else
{
return v1.x == v2.x && v1.y == v2.y;
}
}
public static bool operator !=(Vector v1, Vector v2)
{
if (v1 as object == null || v2 as object == null)
{
if (v1 as object == null && v2 as object == null)
{
return true;
}
return false;
}
else
{
return v1.x != v2.x || v1.y != v2.y;
}
}
public override bool Equals(object obj)
{
try
{
return this == obj as Vector;
}
catch (System.Exception)
{
return false;
}
}
public override string ToString() => $"({x}, {y})";
}
}