The console draws the sine/cosine curve

Source: Internet
Author: User
Tags acos asin

Cosine curve

Of course, we don't use arrays here ... Otherwise, there is no technical content.

Sine curve is characterized by symmetry, and the left part of the ordinate is from 11 straight down to 1, the corresponding right of the ordinate is from 11 to 1. The difficulty is that the display can only be output by line, that is, the output of a row can no longer turn the output on a line of information.

We customize a scaling factor A of 10, with ACOs (-1) *10 can be equal to 31, so the total width is 62, the starting point of the horizontal axis is 0, the end of the horizontal axis of 61. Because the cosine curve is symmetrical, so in the 0~180度, that is, the left part, the horizontal axis is the same as the corresponding ordinate 180~360度, that is, the right part of the horizontal axis (62-k).

#include <stdio.h>#include <math.h>intMain () {DoubleYintX,k; for(y=1; y>=-1; y-=0.1) {k=ACOs(y) *Ten; for(x=1; x<k;x++)printf(" ");printf("*"); for(;x< +-k;x++)printf(" ");printf("*\n"); }return 0;}

Sinusoidal

The sine curve is much more difficult ... We continue to use the total width (62) In the cosine curve example.

I believe that for the left part of the people can easily ...

        if(1<=k&&k<=15)        {            for(x=1;x<k;x++)                printf(" ");            printf("*");            for(;x<30-k;x++)                printf(" ");            printf("*\n");        }

The right part of me also made a long time, mainly ignored ASIN (-1) *10 is negative ... Hey

So after discovering this, you should write the IF statement like this:

if(-15<=k&&k<=-1)

But it's also important to note that we're not using arrays to print asterisks, in other words, for the horizontal axis, printing starts at 1, and our coordinates are negative.

We should convert it: the negative number k into (31-k) it becomes a reasonable positive number, the same, in the 270 degrees around 90 is also symmetrical. and 270 degrees corresponds to a width of 46, then 180~270度 between the K symmetry to 270~360度 how much? Set up an equation and it's done:

46-(31- k)= x - 46=>x = 61 + k

thus

#include <stdio.h>#include <math.h>intMain () {DoubleYintX,k; for(y=1; y>=-1; y-=0.1) {k=ASIN(y) *Ten;if(1<=k&&k<= the)        { for(x=1; x<k;x++)printf(" ");printf("*"); for(;x< --k;x++)printf(" ");printf("*\n"); }Else if(- the<=k&&k<=-1)        { for(x=1;x< to-k;x++)printf(" ");printf("*"); for(x;x< A+k;x++)printf(" ");printf("*\n"); }    }return 0;}

Source

"Source download, no resource points required"

The console draws the sine/cosine curve

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.