Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我必须创建两种方法,一种是顺时针旋转,另一种是逆时针旋转_Java_Math_Transformation - Fatal编程技术网

Java 我必须创建两种方法,一种是顺时针旋转,另一种是逆时针旋转

Java 我必须创建两种方法,一种是顺时针旋转,另一种是逆时针旋转,java,math,transformation,Java,Math,Transformation,我有两种方法,一种是获取用户输入的点,然后执行计算,以顺时针旋转90度,而另一种方法完全相同,只是逆时针旋转90度。我试过了,但结果不对,有什么建议可以解决吗 private static String Clockwise(int Amount, int[] x, int[] y) { String message = ""; int[] tempX = x; int[] tempY = y; for (int count = 0

我有两种方法,一种是获取用户输入的点,然后执行计算,以顺时针旋转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
私有静态字符串顺时针方向(整数金额,整数[]x,整数[]y){
字符串消息=”;
int[]tempX=x;
int[]tempY=y;

对于(int count=0;count绕原点逆时针旋转一般角度,公式为:

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 = -y
y_new = x
这样做是有好处的,因为像这样的两个作业比做完整的数学要快得多

请注意,您必须小心不要写:

x = -y;
y = x;

因为第二个语句使用第一个语句的结果。您需要将原始的
x
存储在临时变量中才能正确执行此操作。

要围绕原点逆时针旋转一个大致角度,公式为:

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 = -y
y_new = x
这样做是有好处的,因为像这样的两个作业比做完整的数学要快得多

请注意,您必须小心不要写:

x = -y;
y = x;

因为第二个语句使用第一个语句的结果。您需要将原始的
x
存储在一个临时变量中才能正确执行此操作。

您得到了什么结果?您期望得到什么?问题是,您在每一步都围绕一个轴进行镜像-要旋转90度,x坐标来自Y坐标,Y坐标来自X坐标。提供具有预期和实际结果的示例输入。An会有所帮助。请注意,
tempX
tempY
变量没有任何优势-在写入
int[]时,您没有复制数组tempX=x;
。您也可以只使用原始变量。好的-但是如果输入1,您不想旋转它两次。它当前旋转了太多一步。您得到了什么结果?您希望得到什么?问题是,您在每一步上都围绕一个轴进行镜像-要旋转90度,x坐标来自Y坐标e、 Y坐标来自X坐标。提供具有预期和实际结果的示例输入。An会有所帮助。请注意,
tempX
tempY
变量没有任何优势-在写入
int[]时,您没有复制数组tempX=x;
。您也可以只使用原始变量。确定-但如果输入1,则不希望将其旋转两次。它当前将其旋转一步太多。