0
我正在用C++编写一些代码来在一个图形上显示两个图形。一个功能需要是sin
功能,另一个功能需要是cos
功能。用C++创建正弦/余弦图形
我有sin
图和cos
图所需的代码,但我无法让它们一起显示。
#include <cmath>
#include <iostream>
#include <cstdlib>
using namespace std;
const float PI = 3.1459265;
int main()
{
int size = 80, height = 21;
char chart[height][size];
size = 80, height = 21;
double cosx[size];
double sinx[size];
{
for (int i=0; i<size; i++)
cosx[i] = 10*cos(i/4.5);
for (int i=0; i<height; i++)
for (int j=0; j<size; j++)
if (-.01 < 10 - i - round(cosx[j]) && 10 - i - round(cosx[j]) <0.01)
chart[i][j] = 'x';
else if (i==height/2)
chart[i][j] = '-';
else
chart[i][j] = ' ';
for (int i=0; i<height; i++)
for (int j=0; j<size; j++)
cout << chart[i][j];
for (int i=0; i<size; i++)
sinx[i] = 10*sin(i/4.5);
for (int i=0; i<height; i++)
for (int j=0; j<size; j++)
if (-.01 < 10 - i - round(sinx[j]) && 10 - i - round(sinx[j]) <0.01)
chart[i][j] = 'x';
else if (i==height/2)
chart[i][j] = '-';
else
chart[i][j] = ' ';
for (int i=0; i<height; i++)
for (int j=0; j<size; j++)
cout << chart[i][j];
}
}
你有图表库吗?它看起来像你试图输出到控制台。 –
如果你只是想快速绘制一些东西,你可能想看看gnuplot http://gnuplot.sourceforge.net/demo_5.0/simple.html或matplotlib http://matplotlib.org/examples/animation/ basic_example.html – moof2k
我试图格式化代码,但是如果缩进不正确,您应该检查我的编辑。尽管并非总是必要,但围绕代码块使用括号“{..}”可以提高可读性并使将来的编辑更容易。 – Tony