C语言爱心代码大全

一、C语言爱心代码大全,会Ctrl+C就可以表白了!

博主整理了一个C语言爱心代码大全,里面有C语言爱心代码会动的动态效果和C语言爱心代码大全静态效果,只需复制粘贴就可以用啦!

1、动态C语言爱心代码效果图如下:

C语言爱心代码如下:

  1. #include

  2. #include

  3. #include

  4. #include

  5. #include

  6.  
  7. struct MyLove

  8. {

  9. int NUMS; // 编号

  10. double m;

  11. double n;

  12. double size;

  13. bool Is_show;

  14. int x;

  15. int y;

  16. };

  17.  
  18. MyLove mylove[400];

  19. int CenterX = 320;

  20. int CenterY = 180;

  21. double Size = 60;

  22. void initdata(); // 初始化数据

  23. void updata(); // 更新

  24. void movedata(); // 平移

  25. void showdata(); // 显示

  26. int* GetRand(int* buf, int count, int range); // 随机数的生成

  27. void heart(int x0, int y0, int size, COLORREF C);

  28. void HpSleep(int ms);

  29.  
  30. int main()

  31. {

  32. initgraph(640, 480);

  33. initdata();

  34. BeginBatchDraw();

  35. while (true)

  36. {

  37. updata();

  38. showdata();

  39. HpSleep(30); // 改为精确延时

  40. FlushBatchDraw();

  41. cleardevice();

  42. }

  43. EndBatchDraw();

  44. _getch();

  45. return 0;

  46. }

  47.  
  48. void updata()

  49. {

  50. int* buf = (int*)malloc(sizeof(int) * 20);

  51. buf = GetRand(buf, 20, (int)(2 * Size / 0.01));

  52. movedata();

  53. for (int i = 0; i < 20; i++)

  54. {

  55. mylove[i].m = buf[i] * 0.01;

  56. mylove[i].n = (((sin(buf[(int)i] * 0.01) * sqrt(fabs(cos(buf[(int)i] * 0.01)))) / (sin(buf[(int)i] * 0.01) + 1.4142)) - 2 * sin(buf[(int)i] * 0.01) + 2);

  57. mylove[i].size = Size;

  58. mylove[i].NUMS = i / 20;

  59. mylove[i].Is_show = true;

  60. mylove[i].x = (int)(-Size * mylove[i].n * cos(mylove[i].m) + CenterX);

  61. mylove[i].y = (int)(-Size * mylove[i].n * sin(mylove[i].m) + CenterY - mylove[i].size);

  62. }

  63. for (int i = 20; i < 400; i++)

  64. {

  65. mylove[i].size = mylove[i].size + 1;

  66. if (mylove[i].size > 80)

  67. {

  68. mylove[i].size = 80;

  69. }

  70. mylove[i].NUMS = i / 20;

  71. mylove[i].x = (int)(-mylove[i].size * mylove[i].n * cos(mylove[i].m) + CenterX);

  72. mylove[i].y = (int)(-mylove[i].size * mylove[i].n * sin(mylove[i].m) + CenterY - mylove[i].size);

  73. }

  74. }

  75.  
  76. void movedata()

  77. {

  78. for (int i = 399; i > 19; i--)

  79. {

  80. mylove[i] = mylove[i - 20];

  81. }

  82. }

  83.  
  84. void showdata()

  85. {

  86. settextcolor(RED);

  87. wchar_t c = 0x59; // 0x28 是电话机在 Wingdings 字体中的对应编码

  88. for (int i = 0; i < 400; i++)

  89. {

  90. settextstyle(mylove[i].NUMS + 10, 0, "Webdings");

  91. setbkmode(TRANSPARENT);

  92. outtextxy(mylove[i].x + 20, mylove[i].y + 20, c);

  93. }

  94. }

  95.  
  96. int* GetRand(int* buf, int count, int range)

  97. {

  98. struct timeb timeSeed;

  99. ftime(&timeSeed);

  100. srand(timeSeed.time * 1000 + timeSeed.millitm); // milli time

  101. for (int i = 0; i < count; i++)

  102. {

  103. int randTmp = rand() % range;

  104. for (int j = 0; j < i; j++)

  105. {

  106. if (buf[j] == randTmp)

  107. {

  108. break;//检查重复。

  109. }

  110. }

  111. buf[i] = randTmp;

  112. }

  113. return buf;

  114. }

  115.  
  116. void initdata()

  117. {

  118. for (int i = 0; i < 400; i++)

  119. {

  120. mylove[i].NUMS = 0;

  121. mylove[i].m = 0;

  122. mylove[i].n = 0;

  123. mylove[i].size = 0;

  124. mylove[i].Is_show = false;

  125. mylove[i].x = 0;

  126. mylove[i].y = 0;

  127. }

  128. }

  129.  
  130. // 精确延时函数(可以精确到 1ms,精度 ±1ms)

  131. // by yangw80, 2011-5-4

  132. void HpSleep(int ms)

  133. {

  134. static clock_t oldclock = clock(); // 静态变量,记录上一次 tick

  135. oldclock += ms * CLOCKS_PER_SEC / 1000; // 更新 tick

  136. if (clock() > oldclock) // 如果已经超时,无需延时

  137. oldclock = clock();

  138. else

  139. while (clock() < oldclock) // 延时

  140. Sleep(1); // 释放 CPU 控制权,降低 CPU 占用率,精度 10~16ms

  141. // Sleep(0); // 更高精度、更高 CPU 占用率,精度 1ms

  142. }

2、静态C语言爱心代码大全展示效果如下:

f3c4e710980149df80d9e2a633432879.png

C语言爱心代码如下:

 
  1. #include

  2. #include

  3. #include

  4. #include

  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. }

3、love彩色C语言爱心代码大全效果图如下:

4b91f658aa2c41b5980b9be26e42731f.png

C语言爱心代码:

 
  1. /*爱心代码:*/

  2. #include

  3. #include

  4. #include

  5. #include

  6. #define U 0.1

  7. #define V 0.053

  8. void SetColor(unsigned short ForeColor,unsigned short BackGroundColor)

  9. {

  10. HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);

  11. SetConsoleTextAttribute(hCon,(ForeColor%16)|(BackGroundColor%16*16));

  12. }

  13. int main()

  14. {

  15. int i,s=0,t,a=10,b=11,c=12,d=13,e=14;

  16. int z[] = {32,32,206,210,176,174,209,197,209,197,33,32,32};

  17. /*

  18. 用两位ASCⅡ码表示汉字,中间用“,”分隔;

  19. 32,32--表示空格

  20. 206,210--我

  21. 176,174--爱

  22. 209,197--雅

  23.  
  24. (想找ASCⅡ码对应的汉字,可以下载“中英文字符编码查询”小软件,可以点击我的头像到我发布的资源中查找)

  25. */

  26. float x,y;

  27. srand(time(NULL));

  28. for(y=1.3;y>=-1.1;y-=U)

  29. {

  30. for(x=-2;x<1.4;x+=V)

  31. {

  32. if((((x*x+y*y-1)*(x*x+y*y-1)*(x*x+y*y-1)-x*x*y*y*y)<=0))

  33. {

  34. if(y>=1.3-10*U||y<=1.3-11*U)

  35. {

  36. s++;

  37. if(s%4==1){SetColor(a,0);printf("l");}

  38. if(s%4==2){SetColor(e,0);printf("o");}

  39. if(s%4==3){SetColor(c,0);printf("v");}

  40. if(s%4==0){SetColor(d,0);printf("e");}

  41. }

  42. else

  43. {

  44. for(i = 0;i < 42;i++)

  45. {

  46. if(i<=14||i>=28)

  47. {

  48. s++;

  49. if(s%4==1){SetColor(a,0);printf("l");}

  50. if(s%4==2){SetColor(e,0);printf("o");}

  51. if(s%4==3){SetColor(c,0);printf("v");}

  52. if(s%4==0){SetColor(d,0);printf("e");}

  53. }

  54. else

  55. {

  56. SetColor(b,0);

  57. printf("%c", z[i-15]);

  58. Sleep(50);

  59. }

  60. }

  61. break;

  62. }

  63. }

  64. else

  65. printf(" ");

  66. Sleep(1);

  67. }

  68. printf("\n");

  69. }

  70. printf("l love you");

  71. getchar();

  72. while(1)

  73. {

  74. system("cls");

  75. t=a;a=b;b=c;c=d;d=e;e=t;

  76. for(y=1.3;y>=-1.1;y-=U)

  77. {

  78. for(x=-2;x<1.4;x+=V)

  79. {

  80. if((((x*x+y*y-1)*(x*x+y*y-1)*(x*x+y*y-1)-x*x*y*y*y)<=0))

  81. {

  82. if(y>=1.3-10*U||y<=1.3-11*U)

  83. {

  84. s++;

  85. if(s%4==1){SetColor(a,0);printf("l");}

  86. if(s%4==2){SetColor(b,0);printf("o");}

  87. if(s%4==3){SetColor(c,0);printf("v");}

  88. if(s%4==0){SetColor(d,0);printf("e");}

  89. }

  90. else

  91. {

  92. for(i = 0;i < 42;i++)

  93. {

  94. if(i<=14||i>=28)

  95. {

  96. s++;

  97. if(s%4==1){SetColor(a,0);printf("l");}

  98. if(s%4==2){SetColor(b,0);printf("o");}

  99. if(s%4==3){SetColor(c,0);printf("v");}

  100. if(s%4==0){SetColor(d,0);printf("e");}

  101. }

  102. else

  103. {

  104. SetColor(e,0);

  105. printf("%c", z[i-15]);

  106. }

  107. }

  108. break;

  109. }

  110. }

  111. else

  112. printf(" ");

  113. }

  114. printf("\n");

  115. }

  116. Sleep(1000);

  117. system("cls");

  118.  
  119. }

  120. }

4、红色雪花C语言爱心代码大全图案

66a8f39b52e84f70a7abc0dbfd1d2541.png

C语言爱心代码:

 
  1. #include

  2. #include

  3.  
  4. int main()

  5. {

  6. float x,y,a;

  7. for(y=1.5; y>-1.5; y-=0.1)

  8. {

  9. for(x=-1.5; x<1.5; x+=0.05)

  10. {

  11. a=x*x+y*y-1;

  12. putchar(a*a*a-x*x*y*y*y<=0.0?'*':' ');

  13. }

  14. system("color 0c");

  15. putchar('\n');

  16. }

  17. return 0;

  18. }

5、C语言爱心代码大全—里面含有动态生日快乐

C语言爱心代码:

 
  1. #include

  2. #include

  3. #include

  4. #define I 20

  5. #define R 340

  6. int main()

  7. {

  8. system("mode con cols=80 lines=60 ");

  9. system("title 向你致以最诚挚的祝福!");

  10. char data[200][60] = { {"祝你生日快乐!祝你生日快乐 !"},{"\nHappy birthday to you!\n"},{"Happy birthday my dear friend!\n"},

  11. {"在你面前的台里本,总有一天是很独特!\n"},{"虽然你和我,没办法选,来到世界的某刻。\n"},{"天气寒冷,又或炎热,又或者什么血型和星座!\n"},

  12. {"每年这刻,我对自己说,有生的日子要更快乐!\n"},{"天气寒冷,又或炎热,又或者什么血型和星座!\n"},{"每年这刻,我对自己说,有生的日子要更快乐!\n"},

  13. {"爸爸妈妈辛苦了!谢谢你们为我付出太多!\n"},{"生日祝福,吹灭蜡烛,我要我自己不许哭。 \n"},{"长大了一岁变得靠谱,继续走自己的旅程!\n"},

  14. {"又到这一个时刻,幸福的花火,手机祝福收到很多很多。\n"},{"看到最后一条来自你,写着让我感动的话语。\n"},{"亲爱的朋友要经常联络,我们一起来唱这首歌!\n"},

  15. {"祝你生日快乐!我们一起快乐!\n"}

  16. };

  17. int sign = 0;

  18. int i, j, e;

  19. int a;

  20. long time;

  21. for (i = 1, a = I; i < I / 2; i++, a--)

  22. {

  23. for (j = (int)(I - sqrt(I * I - (a - i) * (a - i))); j > 0; j--)

  24. printf(" ");

  25. for (e = 1; e <= 2 * sqrt(I * I - (a - i) * (a - i)); e++)

  26. printf("\3");

  27. for (j = (int)

  28. (2 * (I - sqrt(I * I - (a - i) * (a - i)))); j > 0; j--)

  29. printf(" ");

  30. for (e = 1; e <= 2 * sqrt(I * I - (a - i) * (a - i)); e++)

  31. printf("\3");

  32. printf("\n");

  33. }

  34. for (i = 1; i < 80; i++)

  35. {

  36. if (i == 25)

  37. {

  38. printf("======祝皮皮龙,生日快乐。======");

  39. i += 30;

  40. }

  41. printf("\3");

  42. }

  43. printf("\n");

  44. for (i = 1; i <= R / 2; i++)

  45. {

  46. if (i % 2 || i % 3)

  47. continue;

  48. for (j = (int)(R - sqrt(R * R - i * i)); j > 0; j--)

  49. printf(" ");

  50. for (e = 1; e <= 2 * (sqrt(R * R - i * i) - (R - 2 * I)); e++)

  51. printf("\3");

  52. printf("\n");

  53. }

  54.  
  55. for (; ; )

  56. {

  57. system("color a");

  58. for (time = 0; time < 99999999; time++); system("color b");

  59. for (time = 0; time < 99999999; time++); system("color c");

  60. for (time = 0; time < 99999999; time++); system("color d");

  61. for (time = 0; time < 99999999; time++); system("color e");

  62. for (time = 0; time < 99999999; time++); system("color f");

  63. for (time = 0; time < 99999999; time++);

  64. system("color 0"); for (time = 0; time < 99999999; time++);

  65. system("color 1"); for (time = 0; time < 99999999; time++);

  66. system("color 2"); for (time = 0; time < 99999999; time++);

  67. system("color 3"); for (time = 0; time < 99999999; time++);

  68. system("color 4");

  69. for (time = 0; time < 99999999; time++); system("color 5");

  70. for (time = 0; time < 99999999; time++); system("color 6");

  71. for (time = 0; time < 99999999; time++); system("color 7");

  72. for (time = 0; time < 99999999; time++); system("color 8");

  73. for (time = 0; time < 99999999; time++); system("color 9");

  74. for (time = 0; time < 99999999; time++); system("color ab");

  75. for (time = 0; time < 99999999; time++); system("color ac");

  76. for (time = 0; time < 99999999; time++); system("color ad");

  77. for (time = 0; time < 99999999; time++); system("color ae");

  78. for (time = 0; time < 99999999; time++); system("color af");

  79. for (time = 0; time < 99999999; time++);

  80. printf("%s", data[sign]);

  81. sign++;

  82. }

  83. return 0;

  84. }

6、love组成C语言爱心代码

8811618d9b014b569d8738923026fc1a.png

C语言爱心代码:

 
  1. #include

  2. int main()

  3. {

  4. int i, j, k, n = 0, x = 0, y = 50;

  5.  
  6. //爱心的头部没有规律,所以直接打印

  7. printf("\n\n\n\n\n");

  8. printf(" lovelove lovelove\n");

  9. printf(" lovelovelove lovelovelove\n");

  10. printf(" lovelovelovelove lovelovelovelove\n");

  11. printf(" lovelovelovelovelove lovelovelovelovelove\n");

  12. printf(" lovelovelovelovelovelo lovelovelovelovelovelo\n");

  13. printf(" lovelovelovelovelovelove lovelovelovelovelovelov\n");

  14. for (i = 0; i < 2; i++)

  15. {

  16. printf("lovelovelovelovelovelovelovelovelovelovelovelovelove\n");

  17.  
  18. }

  19.  
  20. for(i=0;i<5;i++) //爱心的中间部分的上部分

  21. {

  22. y = 50;

  23. y = y - i*2;

  24. n++;

  25. for (k = 0; k < n; k++) //在每一行的起始位置先打印空格

  26. {

  27. printf(" ");

  28. }

  29. while(1) //空格后面打印love,但是要注意love即使没打印完,也要换行

  30. {

  31. if (x < y)

  32. {

  33. printf("l");

  34. y--;

  35. }

  36. else

  37. break;

  38. if (x < y)

  39. {

  40. printf("o");

  41. y--;

  42. }

  43. else

  44. break; if (x < y)

  45. {

  46. printf("v");

  47. y--;

  48. }

  49. else

  50. break; if (x < y)

  51. {

  52. printf("e");

  53. y--;

  54. }

  55. else

  56. break;

  57. }

  58. printf("\n");

  59. }

  60.  
  61.  
  62. //最下面的部分,具体内容同上,没和上一部分放一起是因为从这行开始多两个空格

  63. for (i = 0,n=3; i < 10; i++)

  64. {

  65. y = 37;

  66. y = y - i * 4;

  67. n++;

  68. for (k = 0; k < n; k++)

  69. {

  70. printf(" ");

  71. }

  72. while (1)

  73. {

  74. if (x < y)

  75. {

  76. printf("l");

  77. y--;

  78. }

  79. else

  80. break;

  81. if (x < y)

  82. {

  83. printf("o");

  84. y--;

  85. }

  86. else

  87. break; if (x < y)

  88. {

  89. printf("v");

  90. y--;

  91. }

  92. else

  93. break; if (x < y)

  94. {

  95. printf("e");

  96. y--;

  97. }

  98. else

  99. break;

  100. }

  101. printf("\n");

  102. }

  103.  
  104. printf("\n\n\n\n\n\n\n\n\n\n\n\n");

  105. return 0;

  106. }

 7、 红色玫瑰C语言代码大全

68115d9a285e49f1b9da0f42ed5499bd.png

 代码:

 
  1. #include

  2. #include

  3. #include

  4. const int max_iterations = 128;

  5.  
  6. const float stop_threshold = 0.01f;

  7.  
  8. const float grad_step = 0.01f;

  9.  
  10. const float clip_far = 10.0f;

  11.  
  12. const float PI = 3.14159265359f;

  13.  
  14. const float PI2 = 6.28318530718f;

  15.  
  16. const float DEG_TO_RAD = PI / 180.0f;

  17.  
  18. typedef struct { float x, y; } vec2;

  19.  
  20. typedef struct { float x, y, z; } vec3;

  21.  
  22. typedef struct { float m[9]; } mat3;

  23.  
  24. const vec3 light_pos = { 20.0f, 50.0f, 20.0f };

  25.  
  26. float min(float a, float b) { return a < b ? a : b; }

  27.  
  28. float max(float a, float b) { return a > b ? a : b; }

  29.  
  30. float clamp(float f, float a, float b) { return max(min(f, b), a); }

  31.  
  32. vec2 make2(float x, float y) { vec2 r = { x, y }; return r; }

  33.  
  34. vec2 add2(vec2 a, vec2 b) { vec2 r = { a.x + b.x, a.y + b.y }; return r; }

  35.  
  36. vec2 sub2(vec2 a, vec2 b) { vec2 r = { a.x - b.x, a.y - b.y }; return r; }

  37.  
  38. float dot2(vec2 a, vec2 b) { return a.x * b.x + a.y * b.y; }

  39.  
  40. float length2(vec2 v) { return sqrt(dot2(v, v)); }

  41.  
  42. vec3 make3(float x, float y, float z) { vec3 r = { x, y, z }; return r; }

  43.  
  44. vec3 add3(vec3 a, vec3 b) { vec3 r = { a.x + b.x, a.y + b.y, a.z + b.z }; return r; }

  45.  
  46. vec3 sub3(vec3 a, vec3 b) { vec3 r = { a.x - b.x, a.y - b.y, a.z - b.z }; return r; }

  47.  
  48. vec3 mul3(vec3 a, vec3 b) { vec3 r = { a.x * b.x, a.y * b.y, a.z * b.z }; return r; }

  49.  
  50. vec3 scale3(vec3 v, float s) { vec3 r = { v.x * s, v.y * s, v.z * s }; return r; }

  51.  
  52. float dot3(vec3 a, vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }

  53.  
  54. float length3(vec3 v) { return sqrt(dot3(v, v)); }

  55.  
  56. vec3 normalize3(vec3 v) { return scale3(v, 1.0f / length3(v)); }

  57.  
  58. vec3 mul(mat3 m, vec3 v) {

  59. return make3(

  60. m.m[0] * v.x + m.m[3] * v.y + m.m[6] * v.z,

  61. m.m[1] * v.x + m.m[4] * v.y + m.m[7] * v.z,

  62. m.m[2] * v.x + m.m[5] * v.y + m.m[8] * v.z);

  63. }

  64. mat3 rotationXY(float x, float y) {

  65. vec2 c = { cos(x), cos(y) }, s = { sin(x), sin(y) };

  66. mat3 m = {

  67. c.y , 0.0f, -s.y,

  68. s.y * s.x, c.x, c.y * s.x,

  69. s.y * c.x, -s.x, c.y * c.x

  70. };

  71. return m;

  72. }

  73. float opI(float d1, float d2) { return max(d1, d2); }

  74.  
  75. float opU(float d1, float d2) { return min(d1, d2); }

  76.  
  77. float opS(float d1, float d2) { return max(-d1, d2); }

  78.  
  79. float sdPetal(vec3 p, float s)

  80. {

  81. p = add3(mul3(p, make3(0.8f, 1.5f, 0.8f)), make3(0.1f, 0.0f, 0.0f));

  82. vec2 q = make2(length2(make2(p.x, p.z)), p.y);

  83.  
  84. float lower = length2(q) - 1.0f;

  85. lower = opS(length2(q) - 0.97f, lower);

  86.  
  87. lower = opI(lower, q.y);

  88. float upper = length2(sub2(q, make2(s, 0.0f))) + 1.0f - s;

  89.  
  90. upper = opS(upper, length2(sub2(q, make2(s, 0.0f))) + 0.97f - s);

  91.  
  92. upper = opI(upper, -q.y);

  93. upper = opI(upper, q.x - 2.0f);

  94. float region = length3(sub3(p, make3(1.0f, 0.0f, 0.0f))) - 1.0f;

  95. return opI(opU(upper, lower), region);

  96. }

  97. float map(vec3 p) {

  98. float d = 1000.0f, s = 2.0f;

  99. mat3 r = rotationXY(0.1f, PI2 * 0.618034f);

  100. r.m[0] *= 1.08f; r.m[1] *= 1.08f; r.m[2] *= 1.08f;

  101. r.m[3] *= 0.995f; r.m[4] *= 0.995f; r.m[5] *= 0.995f;

  102. r.m[6] *= 1.08f; r.m[7] *= 1.08f; r.m[8] *= 1.08f;

  103. for (int i = 0; i < 21; i++) {

  104. d = opU(d, sdPetal(p, s));

  105. p = mul(r, p);

  106. p = add3(p, make3(0.0, -0.02, 0.0));

  107. s *= 1.05f;

  108. }

  109. return d;

  110. }

  111. vec3 gradient(vec3 pos) {

  112. const vec3 dx = { grad_step, 0.0, 0.0 };

  113. const vec3 dy = { 0.0, grad_step, 0.0 };

  114. const vec3 dz = { 0.0, 0.0, grad_step };

  115. return normalize3(make3(

  116. map(add3(pos, dx)) - map(sub3(pos, dx)),

  117. map(add3(pos, dy)) - map(sub3(pos, dy)),

  118. map(add3(pos, dz)) - map(sub3(pos, dz))));

  119. }

  120. float ray_marching(vec3 origin, vec3 dir, float start, float end) {

  121. float depth = start;

  122. for (int i = 0; i < max_iterations; i++) {

  123. float dist = map(add3(origin, scale3(dir, depth)));

  124. if (dist < stop_threshold)

  125. return depth;

  126. depth += dist * 0.3;

  127. if (depth >= end)

  128. return end;

  129. }

  130. return end;

  131. }

  132. float shading(vec3 v, vec3 n, vec3 eye) {

  133. vec3 ev = normalize3(sub3(v, eye));

  134. vec3 vl = normalize3(sub3(light_pos, v));

  135. float diffuse = dot3(vl, n) * 0.5f + 0.5f;

  136. vec3 h = normalize3(sub3(vl, ev));

  137. float rim = pow(1.0f - max(-dot3(n, ev), 0.0f), 2.0f) * 0.15f;

  138. float ao = clamp(v.y * 0.5f + 0.5f, 0.0f, 1.0f);

  139. return (diffuse + rim) * ao;

  140. }

  141. vec3 ray_dir(float fov, vec2 pos) {

  142. vec3 r = { pos.x, pos.y, -tan((90.0f - fov * 0.5f) * DEG_TO_RAD) };

  143. return normalize3(r);

  144. }

  145.  
  146. float f(vec2 fragCoord) {

  147. vec3 dir = ray_dir(45.0f, fragCoord);

  148. vec3 eye = { 0.0f, 0.0f, 4.5f };

  149. mat3 rot = rotationXY(-1.0f, 1.0f);

  150. dir = mul(rot, dir);

  151. eye = mul(rot, eye);

  152.  
  153. float depth = ray_marching(eye, dir, 0.0f, clip_far);

  154. vec3 pos = add3(eye, scale3(dir, depth));

  155. if (depth >= clip_far)

  156. return 0.0f;

  157. else

  158. return shading(pos, gradient(pos), eye);

  159. }

  160. int main() {

  161. puts("TX");

  162. for (int y = 0; y < 80; y++) {

  163. for (int x = 0; x < 160; x++)

  164. putchar(" .,-:;+=*#@"[(int)(f(make2((x / 160.0f - 0.5f) * 2.0f, (y / 80.0f - 0.5f) * -2.0f)) * 12.0f)]);

  165. putchar('\n');

  166. }

  167.  
  168. printf(" 一见钟情,不是看了一眼就喜欢上了你。而是在看了一眼之后,就再也没有忘记过你。-----ZXY");

  169. system("color 0c");

  170. return 0;

  171. }

你可能感兴趣的:(汇编,开发语言,青少年编程,编辑器,c语言)