How can I check if a matrix is axis aligned? Therefor I mean any rotation a multiple of 90 degrees (including 0).
Right now the best I came up with is creating 2 points that are axis aligned, pass them through the matrix and see if the points are still axis aligned.
// return 0, 90, 180 or 270 if axis aligned, else returns -1
static public int test_matrix_axis_aligned(Mat3 m) {
int rot = -1;
Vec2 a = create_vec2(0, 0);
Vec2 b = create_vec2(1000, 0);
a = mult(m, a);
b = mult(m, b);
// TODO rounding with a certain threshold?
a.x = round(a.x);
a.y = round(a.y);
b.x = round(b.x);
b.y = round(b.y);
boolean axis_aligned = a.x == b.x || a.y == b.y;
if (axis_aligned) {
//float angle = atan2(b.y - a.y, b.x - a.x);
//println("a: "+angle);
//if (angle < 0) angle += PI; // wrong for 270
//rot = (int) round(degrees(angle));
float dx = a.x - b.x;
float dy = a.y - b.y;
if (dx < 0 && dy == 0) {
rot = 0;
} else if (dx == 0 && dy < 0) {
rot = 90;
} else if (dx > 0 && dy == 0) {
rot = 180;
} else if (dx == 0 && dy > 0) {
rot = 270;
}
}
return rot;
}
But I wonder if it can be done more efficient.