绘制余弦曲线和直线

在屏幕上显示0~360度的cos(x)曲线与直线x=f(y)=45*(y-1)+31的迭加图形。其中cos(x)图形用“*”表示,f(y)用“+”表示,在两个图形相交的点上则用f(x)图形的符号。 

#include <cstdio>
#include <cmath>

#define PERIODE 64
#define ENLARGE 10

int
main(void)
{
    int    layer;
    double y;      //ordinate of current layer
    int    x;      //abscissa of current layer
    int    m;      //abscissa of cos(x)
    int    n;      //abscissa of line

    for(layer=0;layer<=20;++layer)
    {
        y=0.1*layer;
        m=(int)(acos(1-y)*ENLARGE);
        n=(int)(45*(y-1)+31);

        for(x=0;x<PERIODE;++x)
        {
                 if(x==m && x==n)       printf("+"); //cross pointer
            else if(x==n)               printf("+"); //on line
            else if(x==m||x==PERIODE-m) printf("*"); //on cos(x)
            else                        printf(" "); //blank
        }
                                        printf("\n");//next line
    }

    return 0;
}

 

Output:

*                                                               
    *                                                       *   
      *                                                   *     
+      *                                                 *      
    +    *                                             *        
        + *                                           *         
           * +                                       *          
            *    +                                  *           
             *        +                            *            
              *           +                       *             
               *               +                 *              
                *                  +            *               
                 *                      +      *                
                  *                         + *                 
                   *                         *   +              
                    *                       *        +          
                      *                   *               +     
                       *                 *                    + 
                        *               *                       
                          *           *                         
                               * *             

posted on 2010-07-27 16:10  sohu2000000  阅读(274)  评论(0编辑  收藏  举报

导航