"Mały" OT: temat natchnął mnie do napisania "odwracalnej" tablicy (np. zgodnie z ruchem wskazówek zegara).
public enum Angle
{
None = 0,
Clockwise90 = 1,
Reversed = 2,
CounterClockwise90 = 3
}
public class MyArray<T>
{
T[,] array;
Angle Rotation
{ get; set; }
public MyArray(int width, int height)
{
this.array = new T[width, height];
}
public MyArray(T[,] array)
{
this.array = array;
}
public void Rotate(Angle a)
{
int rot = (int)Rotation;
rot += (int)a;
rot %= 4;
Rotation = (Angle)rot;
}
public int Width
{ get { return array.GetLength(0); } }
public int Height
{ get { return array.GetLength(1); } }
public T this[int x, int y]
{
get
{
switch (Rotation)
{
case Angle.None: return array[x, y];
case Angle.Clockwise90: return array[Width - x - 1, y];
case Angle.Reversed: return array[Width - x - 1, Height - y - 1];
case Angle.CounterClockwise90: return array[x, Height - y - 1];
default: return default(T);
}
}
set
{
switch (Rotation)
{
case Angle.None: array[x, y] = value; break; ;
case Angle.Clockwise90: array[Width - x, y] = value; break; ;
case Angle.Reversed: array[Width - x, Height - y] = value; break;
case Angle.CounterClockwise90: array[x, Height - y] = value; break;
}
}
}
}