Created
September 30, 2016 10:59
-
-
Save nobodyzxc/4740f37c51564a0e9bab60e02d27c59b 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
//改自 如何用C语言画一个“心形”? - Milo Yip 的回答 | |
//https://www.zhihu.com/question/20187195/answer/34873279 | |
//来个 g++ linux 支援控制代码 改丑了不要找我,不喜勿喷 <(_ _)> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <unistd.h> | |
#include <signal.h> | |
float f(float x, float y, float z) { | |
float a = x * x + 9.0f / 4.0f * y * y + z * z - 1; | |
return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z; | |
} | |
float h(float x, float z) { | |
for (float y = 1.0f; y >= 0.0f; y -= 0.001f) | |
if (f(x, y, z) <= 0.0f) | |
return y; | |
return 0.0f; | |
} | |
void endProg(int msg){ | |
printf("\33[?25h"); | |
exit(0); | |
return; | |
} | |
int main() { | |
char buffer[25][80] = { 0 }; | |
char ramp[] = ".:-=+*#%@"; | |
signal(SIGINT, endProg); | |
printf("\033[?25l"); | |
for (float t = 0.0f;; t += 0.1f) { | |
int sy = 0; | |
float s = sinf(t); | |
float a = s * s * s * s * 0.2f; | |
for (float z = 1.3f; z > -1.2f; z -= 0.1f) { | |
char *p = &buffer[sy++][0]; | |
float tz = z * (1.2f - a); | |
for (float x = -1.5f; x < 1.5f; x += 0.05f) { | |
float tx = x * (1.2f + a); | |
float v = f(tx, 0.0f, tz); | |
if (v <= 0.0f) { | |
float y0 = h(tx, tz); | |
float ny = 0.01f; | |
float nx = h(tx + ny, tz) - y0; | |
float nz = h(tx, tz + ny) - y0; | |
float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz); | |
float d = (nx + ny - nz) * nd * 0.5f + 0.5f; | |
*p++ = ramp[(int)(d * 5.0f)]; | |
} | |
else | |
*p++ = ' '; | |
} | |
} | |
for (sy = 0; sy < 25; sy++) { | |
printf("\033[40;31m"); | |
fwrite(buffer[sy] , 79 , 1 , stdout); | |
printf("\033[0m\n"); | |
} | |
printf("\033[25A"); | |
usleep(6000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment