在文本中绘制曲线

Graphing Curves in Text

#include <stdio.h>
#include <math.h>
void plotline (float y)
{
    char c='@';
    if (x==0.0)
        printf("%c\n",c);
    else if (x==1.0)
        printf("%41c\n",c);

}
void plot ()
{
    float y,x;
    for (x=0.0;x<=3.2;x+=0.2)
    {
        y=sin(x)*sin(x);
        plotline(y);
    }
}
void main()
{
    plot();
}

我正在尝试根据绘图上的一些给定值打印 sin^2(x) 的图形曲线,但到目前为止我得到了这个输出,

@
@
@
@
@
@
@
@
@
@
@
@
@
@
@
@

我希望第一个 @ 在 0,0 上显示,然后每个总和在符号 (@) 上打印另一个,使其看起来像 sin^2(x) 曲线。我目前正在尝试使用二维数组修复它,但它似乎也不起作用。

expected output
@
 @
      @
            @
                    @
                            @
                                  @
                                      @
                                       @
                                     @
                                 @
                          @
                  @
          @
    @
@

比较浮点数之间的等价性很棘手。 x 可能永远不会正好是 1.0。同样作为解决方案,您可以尝试这个

void plotline (float x)
{
    int n = x * 50;
    printf("%*c%c\n", n, ' ', '@'); 
}