Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- float linspace(float minv, float maxv, int nsteps, int n){
- return (maxv - minv)/ (float) nsteps * n + minv;
- }
- float rlinspace(float minv, float maxv, int nsteps, float n){
- return (nsteps * (n - minv) ) / (maxv - minv);
- }
- int calc_index(int w, int i, int j){
- return j * w + i;
- }
- int main()
- {
- int console_w = 79, console_h = 20;
- float xmin = -2*M_PI, xmax = 2*M_PI;
- float ymin = -1.2, ymax = 1.2;
- int i, j;
- char x_axis_s = '|';
- char y_axis_s = '-';
- char empty_space_s = ':';
- char function_s = '*';
- // allocate array
- char char_arr[console_w * console_h];
- memset(char_arr, empty_space_s, console_w * console_h*sizeof(char) );
- // fill the array with axis
- for(i = 0; i < console_w - 1; i++){
- for(j = 0; j < console_h - 1; j++){
- float x = linspace(xmin, xmax, console_w, i);
- float x1 = linspace(xmin, xmax, console_w, i + 1);
- float y = linspace(ymin, ymax, console_h, j);
- float y1 = linspace(ymin, ymax, console_h, j + 1);
- int idx = calc_index(console_w, i, console_h -1 - j);
- if( x <= 0 && x1 > 0){
- char_arr[idx] = x_axis_s;
- }
- if (y <= 0 && y1 > 0){
- char_arr[idx] = y_axis_s;
- }
- }
- }
- // formula to print
- for(i = 0; i < console_w; i++){
- // get x
- float x = linspace(xmin, xmax, console_w, i);
- // calculate y
- float y = sin(x);
- // find where y is in the matrix
- int hc = console_h - rlinspace(ymin, ymax, console_h - 1, y);
- // assign the function symbol
- if(hc > 0 && hc < console_h){
- char_arr[calc_index(console_w, i, hc)] = function_s;
- }
- }
- for(j= 0; j < console_h; j++){
- for(i = 0; i < console_w; i++){
- printf("%c", char_arr[calc_index(console_w, i, j)]);
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment