Last active
November 18, 2022 10:53
-
-
Save techtycho/1da30f80dc4827c544d4b52ab9dedc0c to your computer and use it in GitHub Desktop.
Sine Wave Visualizer (Compatible with any terminal size)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sinloop.c | |
// Compile with '-lm' | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdbool.h> | |
#include <math.h> | |
#include <sys/ioctl.h> | |
#define WIDTH wsize.ws_col | |
struct winsize wsize; | |
float d = 0; | |
int r = 0; | |
int main() { | |
ioctl(STDOUT_FILENO, TIOCGWINSZ, &wsize); // Gets the window size | |
int fill[WIDTH]; | |
// Sets all elements of the array 'fill' to 0 | |
for (int i = 0; i < WIDTH; i++) { | |
fill[i] = 0; | |
} | |
while (1) { | |
r = sin(d) * 10; | |
fill[r + (WIDTH / 2)] = 1; | |
for (int i = 0; i < WIDTH; i++) { | |
if (fill[i]) { | |
printf("O"); | |
} else { | |
printf(" "); | |
} | |
} | |
// Sets all the elements of array 'fill' to 0 again | |
for (int i = 0; i < WIDTH; i++) { | |
fill[i] = 0; | |
} | |
printf("\n"); | |
usleep(100000); // Sleep for one tenth of a second | |
d += 0.5; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Will work only on Linux. (Or any Linux environment like Git Bash and Cygwin).
Change the
sin()
function to acos()
function to get a cosloop!