我该如何用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';
}
这似乎不太正常。
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;
}
sin
函数的负返回值。 - Nico SchertlersinM
的内容的1/4设置为32?这会断开矩阵的前1/4行。 - user3235832