【问题标题】:Rotate sin cos plot by +90 degrees将 sin cos 图旋转 +90 度
【发布时间】:2022-11-11 05:32:07
【问题描述】:

我正在编写一个简单的 java 程序来实现以下输出,即绘制 sin 和 cos,但将输出以间隔旋转 90 度(x 0 到 360,步长为 10)。 sin 用 * 绘制,cos 用 o 绘制。

x=  0      *      o
x= 10       *     o
x= 20        *   o
x= 30         *  o
x= 40          *o
x= 50          o*
x= 60         o  *
x= 70        o   *
x= 80       o     *
x= 90      o      *
x= 100     o       *
x= 110    o       *
x= 120   o        *
x= 130  o        *
x= 140  o        *
x= 150 o        *
x= 160 o       *
x= 170 o      *
x= 180 o     *
x= 190 o    * 
x= 200 o   * 
x= 210 o  *
x= 220 o*
x= 230  *o 
x= 240 *  o
x= 250 *   o 
x= 260 *    o
x= 270 *     o
x= 280 *      o
x= 290 *       o
x= 300 *        o
x= 310  *        o
x= 320  *        o
x= 330   *        o
x= 340    *       o
x= 350     *       o
x= 360      *      o

这是我的代码

class SinCos {
    public static void main(String[] args)
    {
        int numx = 360;
        double numy = 25.0;

    for (double y = 1 ; y >= -1 ; y-=1/numy) {
        double nexty = y-(1/numy);

        for (double x = 0; x <= numx; x+=10) {

            double siny = Math.sin(Math.toRadians(x));
            double cosy = Math.cos(Math.toRadians(x));

            if (siny >= nexty && siny <= y)
                System.out.print('*');
            else
                System.out.print(' ');

            if (cosy >= nexty && cosy <= y)
                System.out.print('o');
            else
                System.out.print(' ');
        }
      System.out.println();
     }
    }
}

如何旋转输出并打印 x 的值作为示例。

【问题讨论】:

    标签: java rotation trigonometry


    【解决方案1】:

    终于设法解决了。 使用 java SinCos a1 a2 platos 运行 即 java SinCos 0 360 25

    class SinCos {
    public static void main(String[] args) {
        int a1, a2, w, platos;
    
        a1 = Integer.parseInt(args[0]);
        a2 = Integer.parseInt(args[1]);
        w = Integer.parseInt(args[2]);
    
        if (args.length != 3) {
            System.out.println("Wrong input");
            System.exit(0);
        }
        if (!(a1 <= a2)) {
            System.out.println("a1 must be <= tou a2");
            System.exit(0);
        }
    
        platos = (w / 2) + 1;
    
        System.out.println();
        for (int x = a1; x <= a2; x += 10) {
    
            int sinValue = (int) Math.round(Math.sin(Math.toRadians(x)) * platos);
            int cosValue = (int) Math.round(Math.cos(Math.toRadians(x)) * platos);
    
            System.out.printf("x= %4s", x + " ");
    
            for (int j = -platos; j <= platos; j++) {
    
                if (j == sinValue)
                    System.out.print("*");
                else
                    System.out.print(' ');
    
                if (j == cosValue) {
    
                    System.out.print("o");
                } else
                    System.out.print(' ');
            }
            System.out.println();
        }
    }
    }
    

    【讨论】: