3

How can I represent the graphic of the sine function inside a c++ matrix with 0 and 1?

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';
}

This doesn't seem to work fine.

Alessandro
  • 175
  • 1
  • 12
  • 5
    Handle the negative return values of `sin`. – Nico Schertler Nov 16 '15 at 14:43
  • why do you set a 1/4 of the contents of `sinM` to 32? that would disconnect the first 1/4 of the rows of your matrix –  Nov 16 '15 at 17:26
  • What are the values of DN and DM? Is M_PI at least double? What do you wnat to do with the negative values? Can you provide an example of the output you want? – Bob__ Nov 16 '15 at 18:03

1 Answers1

7

If you are looking for something like this:

0000000000000000000000000000000000000000000000000000000000000000
0000000000000111111100000000000000000000000000000000000000000000
0000000000111000000011100000000000000000000000000000000000000000
0000000011000000000000011000000000000000000000000000000000000000
0000001100000000000000000110000000000000000000000000000000000000
0000110000000000000000000001100000000000000000000000000000000000
0001000000000000000000000000010000000000000000000000000000000000
0110000000000000000000000000001100000000000000000000000000000000
1000000000000000000000000000000010000000000000000000000000000000
0000000000000000000000000000000001100000000000000000000000000011
0000000000000000000000000000000000010000000000000000000000000100
0000000000000000000000000000000000001100000000000000000000011000
0000000000000000000000000000000000000011000000000000000001100000
0000000000000000000000000000000000000000110000000000000110000000
0000000000000000000000000000000000000000001110000000111000000000
0000000000000000000000000000000000000000000001111111000000000000

then you can use this:

#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;
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
Bob__
  • 12,361
  • 3
  • 28
  • 42