From be89ca12bd796dcbd6543b8a5c206c75ca863f9e Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Wed, 15 Nov 2023 20:36:10 +0100 Subject: [PATCH] added rotate to Layer --- src/lib/turtle/Structure.hx | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/lib/turtle/Structure.hx b/src/lib/turtle/Structure.hx index d0d8c8f..b47f7a6 100644 --- a/src/lib/turtle/Structure.hx +++ b/src/lib/turtle/Structure.hx @@ -39,4 +39,26 @@ abstract Layer(Array>) from Array> { } return str; } + + public function rotate():Layer { + // Transpose the matrix (swap rows and columns) + var transposedMatrix:Array> = []; + for (columnIndex in 0...width()) { + var newRow:Array = []; + for (rowIndex in 0...height()) { + newRow.push(get(columnIndex, rowIndex)); + } + transposedMatrix.push(newRow); + } + + // Reverse the order of the columns + var rotatedMatrix:Array> = []; + for (row in transposedMatrix) { + var rev = row.copy(); + rev.reverse(); + rotatedMatrix.push(rev); + } + + return rotatedMatrix; + } }