added rotate to Layer

This commit is contained in:
Niklas Kapelle 2023-11-15 20:36:10 +01:00
parent ca79714e8d
commit be89ca12bd
Signed by: niklas
GPG Key ID: 4EB651B36D841D16

View File

@ -39,4 +39,26 @@ abstract Layer(Array<Array<Block>>) from Array<Array<Block>> {
} }
return str; return str;
} }
public function rotate():Layer {
// Transpose the matrix (swap rows and columns)
var transposedMatrix:Array<Array<Block>> = [];
for (columnIndex in 0...width()) {
var newRow:Array<Block> = [];
for (rowIndex in 0...height()) {
newRow.push(get(columnIndex, rowIndex));
}
transposedMatrix.push(newRow);
}
// Reverse the order of the columns
var rotatedMatrix:Array<Array<Block>> = [];
for (row in transposedMatrix) {
var rev = row.copy();
rev.reverse();
rotatedMatrix.push(rev);
}
return rotatedMatrix;
}
} }