Display the trigonometric function cos(x) and sin(x) curves from 0 to 360 degrees on the screen with "*"

If you use arrays in a program, this problem is very simple. But if you specify that you cannot use arrays, it's not easy.
The key is that the cosine curve is in the range of 0 ~ 360 degrees, and two points should be displayed in one line. For the general display, it can only be output by line, that is, after the first line of information is output, it can only be output in the next line, and cannot be returned to the previous line. In order to get the graphics required in this article, you must output two "*" in one line at a time.
In order to obtain two points of cosine function cos(x) graph on one line at the same time, the left and right symmetry of cos(x) is considered. If the row direction of the screen is defined as X and the column direction is defined as y, then the graph of 0-180 degrees and the graph of 180-360 degrees are left and right symmetrical. If the total width of the graph is defined as 62 columns, and the coordinate m of Y point is calculated when the row of x is 0-180 degrees, then the coordinate of Y point of 180-360 degrees symmetrical with the same row should be 62-m. In the program, acos is used to calculate the corresponding relation of coordinates (x,y).
C:

#include<stdio.h>
#include<math.h>
#define PI 3.1415926
int main()
{
    double y;;
    int x,m;
    for(y=1;y>=-1;y-=0.1)/*y Is the column direction with values from 1 to - 1 in steps of 0.1*/
    {
        m=acos(y)*10;/*Calculate the radian m corresponding to y, multiply by 10 as the figure magnification*/
        for(x=1;x<m;x++)
        {
            printf(" ");
        }
        printf("*"); /*Controls the * sign on the left side of the print*/
        for(;x<(PI*20-m);x++)
        {
            printf(" ");
        }
        printf("*\n");/*Controls the printing of the symmetrical right * sign in the same line*/
    }
    return 0;
}

C++:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    const int PI=3.1415926;
    double y;
    int x,m;
    for(y=1;y>=-1;y-=0.1)/*y Is the column direction with values from 1 to - 1 in steps of 0.1*/
    {
        m=acos(y)*10;/*Calculate the radian m corresponding to y, multiply by 10 as the figure magnification*/
        for(x=1;x<m;x++)
        {
            cout<<" ";
        }
        cout<<"*";/*Controls the * sign on the left side of the print*/
        for(;x<(PI*20-m);x++)
        {
            cout<<" ";
        }
        cout<<"*\n";/*Controls the printing of the symmetrical right * sign in the same line*/
    }
    return 0;
}

How to display the sin(x) curve of 0 ~ 360 degrees with "*"? The principle is the same as drawing cos(x).
C:

#include<stdio.h>
#include<math.h>
#define PI 3.1415926
int main()
{
    double y;
    int x,m;
    for(y=1;y>=0;y-=0.1)
    {
        m=asin(y)*10;
        for(x=1;x<m;x++)
        {
            printf(" ");
        }
        printf("*");
        for(;x<(PI*10-m);x++)
        {
            printf(" ");
        }
        printf("*\n");
    }
    for(;y>=-1;y-=0.1)
    {
        m=asin(y)*10; /*When y is between - 1 and 0, m is negative, because asin () is (- PI,PI) by default, acos () is (0, 2PI) by default*/
        for(x=1;x<(PI*10-m);x++)
        {
            printf(" ");
        }
        printf("*");
        for(;x<(PI*20+m);x++)
        {
            printf(" ");
        }
        printf("*\n");
    }
    return 0;
}

Posted on Mon, 04 May 2020 21:47:37 -0400 by Petsmacker