Skip to content

Instantly share code, notes, and snippets.

@baba-s
Last active March 15, 2024 04:46
Show Gist options
  • Save baba-s/1cf2f5cd7558c66dae5760bf3f9252a8 to your computer and use it in GitHub Desktop.
Save baba-s/1cf2f5cd7558c66dae5760bf3f9252a8 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
namespace Kogane
{
/// <summary>
/// 配列の拡張メソッド
/// </summary>
public static class ArrayExtensionMethods
{
//================================================================================
// 関数(static)
//================================================================================
/// <summary>
/// 二次元配列のように扱っている一次元配列を時計回りに 90 度回転させたものを返します
/// </summary>
public static (T[] array, int newColumn, int newRow) RotateClockwise<T>
(
this IReadOnlyList<T> self,
int column,
int row,
int rotateCount
)
{
var array = self.ToArray();
var newColumn = column;
var newRow = row;
for ( var i = 0; i < rotateCount; i++ )
{
( array, newColumn, newRow ) = array.RotateClockwise( newColumn, newRow );
}
return ( array, newColumn, newRow );
}
/// <summary>
/// 二次元配列のように扱っている一次元配列を時計回りに 90 度回転させたものを返します
/// </summary>
public static (T[] array, int newColumn, int newRow) RotateClockwise<T>
(
this IReadOnlyList<T> self,
int column,
int row
)
{
var newColumn = row;
var newRow = column;
var array = new T[ column * row ];
for ( var y = 0; y < row; y++ )
{
for ( var x = 0; x < column; x++ )
{
var index = x + y * column;
var newX = row - 1 - y;
var newY = x;
var newIndex = newX + newY * newColumn;
array[ newIndex ] = self[ index ];
}
}
return ( array, newColumn, newRow );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment