Skip to content

Instantly share code, notes, and snippets.

@AxGord
Created April 21, 2024 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AxGord/1eb25ffee5a3db71231f51a7727ec9fe to your computer and use it in GitHub Desktop.
Save AxGord/1eb25ffee5a3db71231f51a7727ec9fe to your computer and use it in GitHub Desktop.
class MatrixRotate {
static function main() {
final w = 3;
final h = 2;
final m = [
1, 0, 2,
0, 3, 4
];
p(r90(m, w, h), h, w);
p(r180(m, w, h), w, h);
p(r270(m, w, h), h, w);
}
static function r90(m:Array<Int>, w:Int, h:Int):Array<Int> {
final r = [];
var d = h;
for (y in 0...h) {
d--;
final offset = y * w;
for (x in 0...w)
r[d + x * h] = m[offset + x];
}
return r;
}
static function r180(m:Array<Int>, w:Int, h:Int):Array<Int> {
final s = w * h;
final r = [];
for (i => v in m)
r[s - 1 - i] = v;
return r;
}
static function r270(m:Array<Int>, w:Int, h:Int):Array<Int> {
final s = w * h;
final r = [];
for (y in 0...h) {
final sourceOffset = y * w;
final destOffset = s - h + y;
for (x in 0...w) {
r[destOffset - x * h] = m[sourceOffset + x];
}
}
return r;
}
static function p(m:Array<Int>, w:Int, h:Int):Void {
trace('------------------');
for (y in 0...h) {
trace([for (x in 0...w) m[y * w + x]].join(' '));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment