首页 > 代码库 > 用C++画心(转)

用C++画心(转)

原地址https://www.zhihu.com/topic/19613730/top-answers

首先上一个动态的心

技术分享

代码如下:

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <windows.h>
 4 #include <tchar.h>
 5 
 6 float f(float x, float y, float z) {
 7     float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
 8     return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
 9 }
10 
11 float h(float x, float z) {
12     for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
13         if (f(x, y, z) <= 0.0f)
14             return y;
15     return 0.0f;
16 }
17 //跳动的心
18 int main() {
19     HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);
20     _TCHAR buffer[25][80] = { _T( ) };
21     _TCHAR ramp[] = _T(".:-=+*#%@");
22 
23     for (float t = 0.0f;; t += 0.1f) {
24         int sy = 0;
25         float s = sinf(t);
26         float a = s * s * s * s * 0.2f;
27         for (float z = 1.3f; z > -1.2f; z -= 0.1f) {
28             _TCHAR* p = &buffer[sy++][0];
29             float tz = z * (1.2f - a);
30             for (float x = -1.5f; x < 1.5f; x += 0.05f) {
31                 float tx = x * (1.2f + a);
32                 float v = f(tx, 0.0f, tz);
33                 if (v <= 0.0f) {
34                     float y0 = h(tx, tz);
35                     float ny = 0.01f;
36                     float nx = h(tx + ny, tz) - y0;
37                     float nz = h(tx, tz + ny) - y0;
38                     float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
39                     float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
40                     *p++ = ramp[(int)(d * 5.0f)];
41                 }
42                 else
43                     *p++ =  ;
44             }
45         }
46 
47         for (sy = 0; sy < 25; sy++) {
48             COORD coord = { 0, sy };
49             SetConsoleCursorPosition(o, coord);
50             WriteConsole(o, buffer[sy], 79, NULL, 0);
51         }
52         Sleep(33);
53     }
54 }

技术分享

代码:

 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 float f(float x, float y, float z) {
 5     float a = x * x + 9.0f / 4.0f * y * y + z * z - 1;
 6     return a * a * a - x * x * z * z * z - 9.0f / 80.0f * y * y * z * z * z;
 7 }
 8 
 9 float h(float x, float z) {
10     for (float y = 1.0f; y >= 0.0f; y -= 0.001f)
11         if (f(x, y, z) <= 0.0f)
12             return y;
13     return 0.0f;
14 }
15 //3D黑白?
16 int main() {
17     for (float z = 1.5f; z > -1.5f; z -= 0.05f) {
18         for (float x = -1.5f; x < 1.5f; x += 0.025f) {
19             float v = f(x, 0.0f, z);
20             if (v <= 0.0f) {
21                 float y0 = h(x, z);
22                 float ny = 0.01f;
23                 float nx = h(x + ny, z) - y0;
24                 float nz = h(x, z + ny) - y0;
25                 float nd = 1.0f / sqrtf(nx * nx + ny * ny + nz * nz);
26                 float d = (nx + ny - nz) * nd * 0.5f + 0.5f;
27                 putchar(".:-=+*#%@"[(int)(d * 5.0f)]);
28             }
29             else
30                 putchar( );
31         }
32         putchar(\n);
33     }
34     getchar();
35 }

 

用C++画心(转)