-
-
Save busterb/f99648f0e46e1f885176 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* Build with 'gcc miku.c -o miku -lncurses -std=c99' | |
*/ | |
#include <stdbool.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <termcap.h> | |
#include <unistd.h> | |
void clear_screen() | |
{ | |
char buf[1024]; | |
char *str; | |
tgetent(buf, getenv("TERM")); | |
str = tgetstr("cl", NULL); | |
fputs(str, stdout); | |
} | |
bool is_in_love(int x, int y, int heart_size) | |
{ | |
const float HEART_COEFFICIENT = 0.7f; | |
const float width = 2.2f; | |
const float height = 3.0f; | |
float check_x = (((float)x / (float)(heart_size)) - 0.5f) * width; | |
float check_y = (((float)(heart_size - y) / (float)heart_size) - 0.4f) * height; | |
float top_y = 0.0f; | |
float bottom_y = 0.0f; | |
if (check_x >= 0) { | |
top_y = sqrt(1 - (check_x * check_x)) + (HEART_COEFFICIENT * sqrt(check_x)); | |
bottom_y = -sqrt(1 - (check_x * check_x)) + (HEART_COEFFICIENT * sqrt(check_x)); | |
} else { | |
top_y = sqrt(1 - (check_x * check_x)) + (HEART_COEFFICIENT * sqrt(-check_x)); | |
bottom_y = -sqrt(1 - (check_x * check_x)) + (HEART_COEFFICIENT * sqrt(-check_x)); | |
} | |
return (bottom_y <= check_y) && (check_y <= top_y); | |
} | |
void draw_heart(int heart_size) | |
{ | |
const char message[] = " SYSTEMATIC LOVE "; | |
int message_indent = ((heart_size / 2) - (sizeof(message) / 4)) - 1; | |
for (int y = 0; y < heart_size; ++y) { | |
for (int x = 0; x < heart_size; ++x) { | |
fputs((is_in_love(x, y, heart_size)) ? "vv" : " ", stdout); | |
if (y == (heart_size / 2) - 1) { | |
if (x == message_indent) { | |
fputs(message, stdout); | |
x += (sizeof(message) / 2); | |
} | |
} | |
} | |
fputs("\n", stdout); | |
} | |
} | |
int main(void) | |
{ | |
const int delay = (1000000 / 30); | |
while (true) { | |
for (int heart_size = 30; heart_size <= 39; ++heart_size) { | |
clear_screen(); | |
draw_heart(heart_size); | |
usleep(delay); | |
} | |
for (int heart_size = 39; heart_size > 30; --heart_size) { | |
clear_screen(); | |
draw_heart(heart_size); | |
usleep(delay); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment