我有两种方法,一种是取用户输入的点,然后执行计算,顺时针旋转90度,而在另一种方法中,它完全相同,只有90度逆时针。我已经尝试了但是我得到了错误的结果,任何关于如何修复它的建议,
private static String Clockwise(int Amount, int[] x, int[] y) {
String message = "";
int[] tempX = x;
int[] tempY = y;
for (int count = 0; count <= Amount; count++) {//amount of times
message = "\n";
for (int index = 0; index < pointer; index++) {//go through array
tempX[index] = tempX[index] * -1;//multiply X
tempY[index] = tempY[index] * 1;//multiply Y
message += ("(" + y[index] + "," + x[index] + ")\n");//output
}//end for
}//end outerfor
return message;
}//end clockwise
private static String AntiClockwise(int Amount, int[] x, int[] y) {
String message = "";
int[] tempX = x;
int[] tempY = y;
for (int count = 0; count <= Amount; count++) {//amount of times
message = "\n";
for (int index = 0; index < pointer; index++) {//go through for loop
tempX[index] = tempX[index] * 1;//multiply X
tempY[index] = tempY[index] * -1;//multiply Y
message += ("(" + tempY[index] + "," + tempX[index] + ")\n");//create message
}//end for
}//end outerfor
return message;
}//end anticlockwise
答案 0 :(得分:2)
要绕原点逆时针旋转一般角度,公式为:
x_new = cos(theta)*x - sin(theta)*y
y_new = sin(theta)*x + cos(theta)*y
因此,如果你想旋转90度的倍数,你可以简单地使用:
double theta = Math.toRadians(90 * N);
并将其替换为公式。这意味着您不需要循环旋转N次 - 只需按实际角度旋转一次。
但是,如果你想在每次旋转时以90度进行,你可以简单地用cos(90度)和sin(90度)的值代替(进行逆时针旋转):
x_new = cos(90 deg)*x - sin(90 deg)*y = 0*x - 1*y = -y
y_new = sin(90 deg)*x + cos(90 deg)*y = 1*x + 0*y = x
因此:
x_new = -y
y_new = x
以这种方式做这样做是有好处的,因为这样的一些任务要比完整的数学运算快得多。
请注意,您必须小心不要写:
x = -y;
y = x;
因为这些语句中的第二个使用了第一个语句的结果。您需要将原始x
存储在临时变量中才能正确执行此操作。