我有一个6x6的矩阵,关键点为G,空点为连字符。 (在0和1 s为无关紧要了这个问题。请注意,防空火炮在左上角的侧面和顶部和连字符的数字只标注轴。)基于矩阵的2D点旋转
// Original Matrix
-
0 ----G-
1 ----L-
2 ----L-
3 ----L-
4 GOOOL-
5 ------
通过使用以下两种方法,我我能够将上述矩阵向左旋转90度。
public static void transpose(int[][] m) {
for (int x = 0; x < m.length; x++) {
for (int y = x; y < m[0].length; y++) {
int temp = m[x][y];
m[x][y] = m[y][x];
m[y][x] = temp;
}
}
}
public static void swapRows(int[][] m) {
for (int i = 0, k = m.length - 1; i < k; ++i, --k) {
int[] x = m[i];
m[i] = m[k];
m[k] = x;
}
}
交换行,然后转置行,产生以下矩阵。 (包括用于以下的说明180度旋转)
// Rotated -90° Matrix // Rotated 180° Matrix
--
0 ------ 0 ------
1 GLLLL- 1 -LOOOG
2 ----O- 2 -L----
3 ----O- 3 -O----
4 ----O- 4 -O----
5 ----G- 5 -G----
在代码中,我也有java.awt.Point中的哪个存储对于g点的位置的一个的ArrayList。矩阵旋转后,必须更新这些点。我可以扫描数组中的G点,尽管每个点必须在ArrayList中保持相同的索引,并且无论以何种方式扫描它,都不能保证原始矩阵中第一个点的索引将保留在与90度或180度矩阵中的第一个点相同的索引。
对于
public ArrayList<java.awt.Point> keyPoints = new ArrayList<>();
keyPoints.add(new Point(0, 4));
keyPoints.add(new Point(4, 0));
ArrayList中我现在有到位旋转点此方法。
它基于以下等式。
for (int i = 0; i < keyPoint.size(); i++) {
Point p = keyPoint.get(i);
double angle = Math.toRadians(-90);
double cos = Math.cos(angle);
double sin = Math.sin(angle);
double x = (p.x * cos) - (p.y * sin);
double y = (p.x * sin) + (p.y * cos);
keyPoint.set(i, new Point((int) x, (int) y));
System.out.printf("Point: (%.2f, %.2f)\n", x, y);
}
对于90度旋转,我目前得到的输出如下
Point: (4.00, 0.00)
Point: (0.00, -4.00)
虽然为90°,我要找输出
Point: (0.00, 1.00)
Point: (4.00, 5.00)
有没有关于执行的任何建议我的循环,(如果不是一个更好的解决方案)?
要旋转90 * n以外的角度吗? – MBo
所有旋转都是90 * n。由于我的矩阵设置,我不相信90 * n以外的旋转是可能的。 – Hidden14
所以你最好用-1/0/1设置一些表格而不是sin/cos计算。 – MBo