Skip to content

Instantly share code, notes, and snippets.

@yves-chevallier
Created December 13, 2019 22:08
Show Gist options
  • Save yves-chevallier/6016a885c929fcbaaf679aa086189049 to your computer and use it in GitHub Desktop.
Save yves-chevallier/6016a885c929fcbaaf679aa086189049 to your computer and use it in GitHub Desktop.
Draw a spiral in text mode
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
#define W (30)
const double pi = 3.1415;
void display(bool tab[W][W]) {
for (int i = 0; i < W; i++) {
for (int j = 0; j < W; j++)
printf("%s ", tab[i][j] ? "•" : " ");
printf("\n");
}
fflush(stdout);
}
void spiral(bool tab[W][W], int k, int r, double o) {
for (int i = 0; i < k; i++) {
double a = (double)W / 2 / k * i;
int x = a * cos(r * pi * i / k + o * 2 * pi) + W / 2;
int y = a * sin(r * pi * i / k + o * 2 * pi) + W / 2;
tab[x][y] = 1;
}
}
int main(void) {
for (int angle = 0; angle < 1000; angle++, usleep(10000)) {
bool t[W][W] = {0};
spiral(t, 200 /* iterations */, 5 /* turns */, angle * 0.01);
display(t);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment