`
atell
  • 浏览: 158403 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

[C趣味算法]画出y=cos(x)和f(x)=45*(y-1)+31的图

阅读更多

 

 

public class testCos {

    /**
     * 画出y=cos(x1)和f(x1)=45*(y-1)+31的图;<br>
     *
     * x1取[0~2*pai],扩大100倍为[0,620];<br>
     * y的范围是[-1,1],扩大100倍为[-100,100]
     */
    public static void main(String[] args) {

        int x1 = 0;
        int x2 = 0;
        int y = 100;

        /*
         * 加减乘除都使用int整数类型; acos的参数依然是[-1,1]的double类型
         */
        for (; y >= -100; y -= 1) {//y取[-10,10]
            x1 = (int) (Math.acos(y / 100.0) * 100);
            x2 = 45 * (y - 100) + 3100;
            for (int i = 0; i <= 620; i++) {//x1取[0,620]
                if (i == x2 && (i == x1 || i == 620 - x1)) {//相交处,使用“+”
                    System.out.print("+");
                } else if (i == x2) {
                    System.out.print("#");
                } else if (i == x1 || i == 620 - x1) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }

            }
            System.out.println();
        }

    }

}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics