当前位置:   article > 正文

爱心代码——c++(借鉴b站up主)_c++爱心代码

c++爱心代码
  1. #include<graphics.h>
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<math.h>
  5. #include<stdlib.h>
  6. //爱心点的结构体
  7. struct Point {
  8. double x, y;//坐标
  9. COLORREF color;//颜色
  10. };
  11. //COLORREF colors[7] = { RGB(255,32,83),RGB(252,222,250),RGB(255,0,0),RGB(255,0,0),RGB(255,2,2),RGB(255,0,8),RGB(255,5,5) };
  12. COLORREF colors[7] = { RGB(55,132,183),RGB(252,222,250),RGB(25,120,130),RGB(25,5,215),RGB(25,230,40),RGB(25,24,112),RGB(255,230,128) };
  13. const int xScreen = 1200;//屏幕宽度
  14. const int yScreen = 800;//屏幕高度
  15. const double PI = 3.1415926;//圆周率
  16. const double e = 2.71828;//自然数e
  17. const double averag_distance = 0.162;// 弧度以0.01增长时,原始参数方程每个点的平均距离
  18. const int quantity = 506;//所用点数
  19. const int frames = 20;//爱心扩张的频率
  20. const int circles = 210;//组成爱心的爱心个数
  21. Point origin_points[quantity];//创建一个保存原始爱心数据的数组
  22. Point points[circles * quantity];//创建一个保存所有爱心的数据数组
  23. IMAGE images[frames];//创建图片数组
  24. double screen_x(double x) {
  25. x += xScreen / 2;
  26. return x;
  27. }//坐标转换为函数
  28. double screen_y(double y) {
  29. y = -y + yScreen / 2;
  30. return y;
  31. }//坐标转换为函数
  32. int creat_random(int x1, int x2) {
  33. if (x2 > x1)
  34. return rand() % (x2 - x1 + 1) + x1;
  35. }//创建x1-x2的随机数函数
  36. void creat_date()
  37. {
  38. int index = 0;
  39. double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  40. for (double radian = 0.1;radian <= 2 * PI;radian += 0.005)
  41. {
  42. x2 = 16 * pow(sin(radian), 3);
  43. y2 = 13 * cos(radian) - 5 * cos(2 * radian) - 2 * cos(3 * radian) - cos(4 * radian);
  44. double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
  45. if (distance > averag_distance) {
  46. x1 = x2, y1 = y2;
  47. origin_points[index].x = x2;
  48. origin_points[index++].y = y2;
  49. }
  50. }
  51. index = 0;
  52. for (double size = 0.1, lightness = 1.5;size <= 20;size += 0.1) {
  53. double success_p = 1 / (1 + pow(e, 8 - size / 2));
  54. if (lightness > 1) lightness -= 0.0025;
  55. for (int i = 0;i < quantity;i++) {
  56. if (success_p > creat_random(0, 100) / 100.0) {
  57. COLORREF color = colors[creat_random(0, 6)];
  58. points[index].color = RGB(GetRValue(color) / lightness, GetGValue(color) / lightness, GetBValue(color) / lightness);
  59. points[index].x = size * origin_points[i].x + creat_random(-4, 4);
  60. points[index++].y = size * origin_points[i].y + creat_random(-4, 4);
  61. }
  62. }
  63. }
  64. index = 0;
  65. for (double size = 0.1;size <= 20;size += 0.1) {
  66. double success_p = 1 / (1 + pow(e, 8 - size / 2));
  67. for (int i = 0;i < quantity;i++) {
  68. if (success_p > creat_random(0, 100) / 100.0) {
  69. points[index].color = colors[creat_random(0, 6)];
  70. points[index].x = size * origin_points[i].x + creat_random(-4, 4);
  71. points[index++].y = size * origin_points[i].y + creat_random(-4, 4);
  72. }
  73. }
  74. }
  75. int points_size = index;
  76. for (int frame = 0;frame < frames;frame++) {
  77. images[frame] = IMAGE(xScreen, yScreen);
  78. SetWorkingImage(&images[frame]);
  79. for (index = 0;index < points_size;index++)
  80. {
  81. double x = points[index].x, y = points[index].y;
  82. double distance = sqrt(pow(x, 2) + pow(y, 2));
  83. double distance_increase = -0.0009 * distance * distance + 0.35714 * distance + 5;
  84. double x_increase = distance_increase * x / distance / frames;
  85. double y_increase = distance_increase * y / distance / frames;
  86. points[index].x += x_increase;
  87. points[index].y += y_increase;
  88. setfillcolor(points[index].color);
  89. solidcircle(screen_x(points[index].x), screen_y(points[index].y), 1);
  90. }
  91. for (double size = 17;size < 23;size += 0.3) {
  92. for (index = 0;index < quantity;index++) {
  93. if ((creat_random(0, 100) / 100.0 > 0.6 && size >= 20) || (size < 20 && creat_random(0, 100) / 100.0>0.95))
  94. {
  95. double x, y;
  96. if (size >= 20) {
  97. x = origin_points[index].x * size + creat_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
  98. y = origin_points[index].y * size + creat_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
  99. }
  100. else {
  101. x = origin_points[index].x * size + creat_random(-5, 5);
  102. y = origin_points[index].x * size + creat_random(-5, 5);
  103. }
  104. setfillcolor(colors[creat_random(0, 6)]);
  105. solidcircle(screen_x(x), screen_y(y), 1);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. int main() {
  112. initgraph(xScreen, yScreen);
  113. BeginBatchDraw();
  114. /*srand(time(0));*/
  115. creat_date();
  116. SetWorkingImage();
  117. bool extend =true, shrink = false;
  118. for (int frame = 0;!_kbhit();) {
  119. putimage(0, 0, &images[frame]);
  120. FlushBatchDraw();
  121. Sleep(20);
  122. cleardevice();
  123. if (extend)
  124. frame == 19 ? (shrink =true, extend = false) : ++frame;
  125. else
  126. frame == 0 ? (shrink = false, extend = true) : --frame;
  127. }
  128. BeginBatchDraw();
  129. closegraph();
  130. return 0;
  131. }

如果打开不了graphics,可能缺少EasyX文件,找官网下一个即可,如果还有更简洁的方式请留言

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/604557
推荐阅读
相关标签
  

闽ICP备14008679号