# 產生 3D 旋轉甜甜圈的甜甜圈形 C 程式碼 ###### tags: `Ascii` ## 簡介 可以在命令列產生一個 3D 旋轉的甜甜圈。 C 程式碼來源影片: {%youtube DEqXNfs_HhY %} ## 完整程式碼 `donut_deobfuscated.c` ```c= #include <stdio.h> #include <math.h> #include <string.h> #define HIDE_CURSOR() printf("\033[?25l") int main() { float A = 0, B = 0; float i, j; int k; float z[1760]; char b[1760]; HIDE_CURSOR(); printf("\x1b[2J"); for(;;) { memset(b,32,1760); memset(z,0,7040); for(j=0; j < 6.28; j += 0.07) { for(i=0; i < 6.28; i += 0.02) { float c = sin(i); float d = cos(j); float e = sin(A); float f = sin(j); float g = cos(A); float h = d + 2; float D = 1 / (c * h * e + f * g + 5); float l = cos(i); float m = cos(B); float n = sin(B); float t = c * h * g - f * e; int x = 40 + 30 * D * (l * h * m - t * n); int y= 12 + 15 * D * (l * h * n + t * m); int o = x + 80 * y; int N = 8 * ((f * e - c * d * g) * m - c * d * e - f * g - l * d * n); if(22 > y && y > 0 && x > 0 && 80 > x && D > z[o]) { z[o] = D; b[o] = ".,-~:;=!*#$@"[N > 0 ? N : 0]; } } } HIDE_CURSOR(); printf("\x1b[H"); for(k = 0; k < 1761; k++) { putchar(k % 80 ? b[k] : 10); A += 0.00004; B += 0.00002; } //usleep(30000); } return 0; } ``` ## 執行方式 P.S. 在 Windows 10 上執行會比較卡,原因未知,建議用 linux 執行會比較順。 ### Windows 10 可以用 cmder,執行指令: ``` gcc -o donut donut_deobfuscated.c -lm && donut.exe ``` ![](https://i.imgur.com/u6HqYdY.png) ### Ubuntu 執行指令: ``` gcc -o donut donut_deobfuscated.c -lm && ./donut ``` ![](https://i.imgur.com/xJNVRp3.png) {%youtube 3b9JXXdwYdY %} ## 其他 ### 用 python 實現的彩色版 Python/Pygame 3D ASCII Spinning Donut Tutorial: {%youtube zn4Yvxww58g %} ### 用 python 實現的 Ascii Earth Python/Pygame 3D ASCII Spinning Earth Tutorial: {%youtube 7Q6yvpjvKVg %}