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

本文介绍了一个Java程序,用于在特定范围内绘制y=cos(x)与f(x)=45*(y-1)+31两个函数的图像。通过调整坐标范围并使用字符来表示函数图像的交点、函数值等,程序能够在控制台输出可视化的函数图像。
AI助手已提取文章相关产品:

 

 

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();
        }

    }

}
 

您可能感兴趣的与本文相关内容

#include <stdio.h> #include <math.h> #include <stdlib.h> #include <windows.h> #include <time.h> #define PI 3.14159265359 char love[20][43] = { "111111111111111111111111111111111111111111", "111111111111110000111111100001000111111111", "110000000000000000011111100011000111111111", "110000111001110001111111000110001111111111", "111100010000110000111111100011000000000011", "111100001000100011111110000100011111100011", "100000000000000000001110000100011111000111", "100000000000000000011100000000110001000111", "100011100011111000110000000000110001111111", "111011100011111100111111000111110001111111", "111000000000000000111111000110000000001111", "111111000111111111111111000110000000000111", "111110001111111111111111000100010001000111", "111000000000000001111111000000010001100011", "111000000011000011111111000000110001100001", "110001110000000111111111000001110001110111", "100011111000011111111111000111110001111111", "111110000000000000011111000111000001111111", "111000000111110000011111000111000011111111", "111111111111111111111111111111111111111111" }; void gotoxy(int x, int y) { HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos = { x,y }; SetConsoleCursorPosition(hout, pos); } void SetColor(int color) { if (color == 0) color = 0x04; HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hCon, color); } void showsnow() { SetColor(rand() % 16); int x = rand() % 120; int y = rand() % 40; gotoxy(x, y); putchar('*'); } float sx, sy; float sdCircle(float px, float py, float r) { float dx = px - sx, dy = py - sy; return sqrtf(dx * dx + dy * dy) - r; } float opUnion(float d1, float d2) { return d1 < d2 ? d1 : d2; } #define T px+scale*r*cosf(theta),py+scale*r*sin(theta) int ribbon() { float x = (fmodf(sy, 0.1f) / 0.1f - 0.5f) * 0.5f; return sx >= x - 0.05f && sx <= x + 0.05f; } float f(float px, float py, float theta, float scale, int n) { float d = 0.0f; for (float r = 0.0f; r < 0.8f; r += 0.02f) d = opUnion(d, sdCircle(T,0.05f *scale * (0.95 - r))); if (n > 0) for (int t = -1; t <= 1; t += 2) { float tt = theta + t * 1.8f; float ss = scale * 0.9f; for (float r = 0.2f;r < 0.8f; r += 0.1f) { d = opUnion(d, f(T, tt, ss * 0.5f, n - 1)); ss *=0.8f; } } return d; } int main(int argc, char* argv[]) { system("mode con cols=120 lines=42"); srand((int)time(NULL)); SetConsoleCP(437); SetConsoleOutputCP(437); int n = argc > 1 ? atoi(argv[1]) : 3; float zoom = argc > 2 ? atof (argv[2]) : 1.0f; for (sy = 0.8f; sy > 0.0f; sy -= 0.02f / zoom, putchar('\n')) { for (sx = -0.35f; sx < 0.35f; sx += 0.01f / zoom) { if (f(0, 0, PI* 0.5f, 1.0f, n) <0.0f) { if (sy < 0.1f) { SetColor(rand() % 16); putchar('\3'); } else { if (ribbon()) { putchar('='); } else { SetColor(rand() % 16); putchar('\3'); } } } else { putchar(' '); } } }
10-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值