如何在0和1的c ++矩阵中表示正弦函数的图形?
char sinM[DN][DM];
memset(sinM,32, sizeof(sinM));
for(int i=1;i<DN;++i) {
double val = sin(i*M_PI/180);
int valY = int(val*DM);
sinM[i][valY] = '1';
}
这似乎没有用。
答案 0 :(得分:7)
如果你正在寻找这样的东西:
0000000000000000000000000000000000000000000000000000000000000000
0000000000000111111100000000000000000000000000000000000000000000
0000000000111000000011100000000000000000000000000000000000000000
0000000011000000000000011000000000000000000000000000000000000000
0000001100000000000000000110000000000000000000000000000000000000
0000110000000000000000000001100000000000000000000000000000000000
0001000000000000000000000000010000000000000000000000000000000000
0110000000000000000000000000001100000000000000000000000000000000
1000000000000000000000000000000010000000000000000000000000000000
0000000000000000000000000000000001100000000000000000000000000011
0000000000000000000000000000000000010000000000000000000000000100
0000000000000000000000000000000000001100000000000000000000011000
0000000000000000000000000000000000000011000000000000000001100000
0000000000000000000000000000000000000000110000000000000110000000
0000000000000000000000000000000000000000001110000000111000000000
0000000000000000000000000000000000000000000001111111000000000000
然后你可以使用它:
#include <iostream>
#include <cmath>
const size_t HEIGHT=16;
const size_t LENGTH=64;
const double pi = std::acos(-1);
int main() {
char board[HEIGHT][LENGTH];
double x_scale = 2.0 * pi / LENGTH;
size_t y_scale = (HEIGHT - 1) / 2;
size_t y;
// initialize
for ( size_t i=0; i<HEIGHT; i++)
for (size_t j=0; j<LENGTH; j++) board[i][j]='0';
// "draw" the function in the matrix
for ( size_t x=0; x<LENGTH; x++) {
y = (size_t)(std::round(y_scale + 1 - std::sin(x*x_scale) * y_scale));
board[y][x] = '1';
}
// print the matrix
for ( size_t i=0; i<HEIGHT; i++) {
for (size_t j=0; j<LENGTH; j++) {
std::cout << board[i][j];
}
std::cout << std::endl;
}
return 0;
}