C language example|Print the cosine curve on the console

need

Print the cosine curve to the console.

Cosine curve, also called cosine wave (cosinwave), is a curve derived from the cosine ratio in mathematical trigonometric functions. Also representative of an analog signal, as opposed to a square wave which represents a digital signal.

The standard pure cosine function formula is:

 example code

/**
 * @author: 冲哥 @C语言中文社区
 * @date: 2023/02/14
 * @description:
 */
#include <stdio.h>
#include <math.h>

#define PI 3.141592653
#define BIG_SIZE 10 //图形放大的倍数

int main() {
    int x;//x轴
    double y;//y轴
    int m;
    for (y = 1; y >= -1; y -= 0.1) {//y的取值范围是-1~1,这里设置步长为0.1
        m = acos(y) * BIG_SIZE;//计算y对应的横坐标的值
        for (x = 1; x < m; x++) {
            printf(" ");//打印*之前的空格
        }
        printf("*");//打印*
        for (; x < 2 * PI * BIG_SIZE - m; x++) {//打印对称面的空格和*
            printf(" ");
        }
        printf("*\n");
    }
}

operation result

 

code analysis

The arccosine function acos() is used to draw the cosine curve. #include <math.h>The header file needs to be introduced in the code. acos(y) calculates the value of the abscissa through the value of the ordinate, determines the value of the abscissa, and the abscissa value of its symmetrical position It can also be determined that the abscissa value obtained by 2 * PIsubtracting, in order to make the printed curve more intuitive, the value of the x-axis in the code is enlarged by 10 times.

For more interesting small projects, see my 哔哩哔哩, Q skirt: Xiaoyu Come Come My Personal Space-Little Fish Come Come My Personal Homepage-Bilibili Video Bilibili Xiaoyu Come Come My Personal Space , providing video, audio, articles, dynamics, favorites and other content shared by Xiaoyu Come, follow Xiaoyu Come Come account, and learn about UP's dynamics as soon as possible. Programming learning group: 725022484 Share a small programming game every day~C/C++ game source code materials and various installation packages, private messages are not often seen! https://space.bilibili.com/1827181878?spm_id_from=333.1007.0.0

Guess you like

Origin blog.csdn.net/yx5666/article/details/129060529