Skip to content

Instantly share code, notes, and snippets.

@Jimmy-Hu
Last active November 4, 2020 03:20
Show Gist options
  • Save Jimmy-Hu/8415c01f81cf500c10c6d607e3272fae to your computer and use it in GitHub Desktop.
Save Jimmy-Hu/8415c01f81cf500c10c6d607e3272fae to your computer and use it in GitHub Desktop.
The full version of PlaneV2 class
public class PlaneV2<T>
{
public int Width { get; } = 0;
public int Height { get; } = 0;
private T[,] Grid = null;
public int Length => Width * Height;
public PlaneV2() : this(1, 1) { }
public PlaneV2(int width, int height)
{
Width = Math.Max(width, 0);
Height = Math.Max(height, 0);
Grid = new T[width, height];
}
public PlaneV2(PlaneV2<T> plane) : this(plane?.Grid) { }
public PlaneV2(T[,] sourceGrid)
{
if (sourceGrid == null)
{
return;
}
Width = sourceGrid.GetLength(0);
Height = sourceGrid.GetLength(1);
Grid = new T[Width, Height];
Array.Copy(sourceGrid, Grid, sourceGrid.Length);
}
public T this[int x, int y]
{
get
{
if (x < 0 || x >= Width)
{
throw new ArgumentOutOfRangeException(nameof(x));
}
if (y < 0 || y >= Height)
{
throw new ArgumentOutOfRangeException(nameof(y));
}
return Grid[x, y];
}
set
{
if (x < 0 || x >= Width)
{
throw new ArgumentOutOfRangeException(nameof(x));
}
if (y < 0 || y >= Height)
{
throw new ArgumentOutOfRangeException(nameof(y));
}
Grid[x, y] = value;
}
}
public PlaneV2<T> SubPlane(int locationX, int locationY, int newWidth, int newHeight)
{
if (this.Grid == null)
{
return null;
}
PlaneV2<T> outputPlaneV2 = new PlaneV2<T>(newWidth, newHeight);
for (int y = 0; y < newHeight; y++)
{
for (int x = 0; x < newWidth; x++)
{
outputPlaneV2[x, y] = this.Grid[locationX + x, locationY + y];
}
}
return outputPlaneV2;
}
public override string ToString() => $"{nameof(PlaneV2<T>)}<{typeof(T).Name}>[{Width}, {Height}]";
public string ToDelimitedStringByHeightThenWidth(string separator = "\t")
{
StringBuilder lines = new StringBuilder();
for (int y = 0; y < Height; y++)
{
StringBuilder columns = new StringBuilder();
string columnDelimiter = "";
for (int x = 0; x < Width; x++)
{
columns.Append(columnDelimiter + Grid[x, y].ToString());
if (columnDelimiter == "")
{
columnDelimiter = separator;
}
}
lines.AppendLine(columns.ToString());
}
return lines.ToString();
}
public string ToDelimitedStringByWidthThenHeight(string separator = "\t")
{
StringBuilder lines = new StringBuilder();
for (int x = 0; x < Width; x++)
{
StringBuilder columns = new StringBuilder();
string columnDelimiter = "";
for (int y = 0; y < Height; y++)
{
columns.Append(columnDelimiter + Grid[x, y].ToString());
if (columnDelimiter == "")
{
columnDelimiter = separator;
}
}
lines.AppendLine(columns.ToString());
}
return lines.ToString();
}
public PlaneV2<OutputT> TransformAll<OutputT>(Func<T, OutputT> lambdaFunction)
{
var outputPlaneV2 = new PlaneV2<OutputT>(this.Width, this.Height);
for (int y = 0; y < this.Height; y++)
{
for (int x = 0; x < this.Width; x++)
{
outputPlaneV2[x, y] = lambdaFunction(this.Grid[x, y]);
}
}
return outputPlaneV2;
}
} // class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment