Skip to content

Instantly share code, notes, and snippets.

@techtycho
Last active November 18, 2022 10:53
Show Gist options
  • Save techtycho/1da30f80dc4827c544d4b52ab9dedc0c to your computer and use it in GitHub Desktop.
Save techtycho/1da30f80dc4827c544d4b52ab9dedc0c to your computer and use it in GitHub Desktop.
Sine Wave Visualizer (Compatible with any terminal size)
// 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;
}
@techtycho
Copy link
Author

techtycho commented Apr 1, 2022

Will work only on Linux. (Or any Linux environment like Git Bash and Cygwin).
Change the sin() function to a cos() function to get a cosloop!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment