20241017-【C、C++】跳动的爱心

[复制链接]
发表于 2026-1-9 03:35:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
结果图片


代码

  1. #include "graphics.h"
  2. #include <conio.h>
  3. #include <time.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. struct Point {
  7.         double x, y;
  8.         COLORREF color;
  9. };
  10. COLORREF colors[256] = {
  11.         RGB(255,32,83),
  12.         RGB(252,222,250),
  13.         RGB(255,0,0),
  14.         RGB(255,0,0),
  15.         RGB(255,2,2),
  16.         RGB(255,0,8),
  17.         RGB(255,5,5),
  18. };
  19. const int xScreen = 1200;
  20. const int yScreen = 800;
  21. const double PI = 3.1426535159;
  22. const double e = 2.71828;
  23. const double averag_distance = 0.162;
  24. const int quantity = 506;
  25. const int circles = 210;
  26. const int frames = 20;
  27. Point origin_points[quantity];
  28. Point points[circles * quantity];
  29. IMAGE images[frames];
  30. double screen_x(double x) {
  31.         x += xScreen / 2;
  32.         return x;
  33. }
  34. double screen_y(double y) {
  35.         y = -y + yScreen / 2;
  36.         return y;
  37. }
  38. int creat_random(int x1, int x2) {
  39.         if (x2 > x1)
  40.                 return rand() % (x2 - x1 + 1) + x1;
  41. }
  42. void creat_data()
  43. {
  44.         int index = 0;
  45.         double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  46.         for (double radian = 0.1; radian <= 2 * PI; radian += 0.005)
  47.         {
  48.                 x2 = 16 * pow(sin(radian), 3);
  49.                 y2 = 13 * cos(radian) - 5 * cos(2 * radian) - 2 * cos(3 * radian) - cos(4 * radian);
  50.                 double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
  51.                 if (distance > averag_distance) {
  52.                         x1 = x2, y1 = y2;
  53.                         origin_points[index].x = x2;
  54.                         origin_points[index++].y = y2;
  55.                 }
  56.         }
  57.         index = 0;
  58.         for (double size = 0.l, lightness = 1.5; size <= 20; size += 0.1)
  59.         {
  60.                 double success_p = 1 / (1 + pow(e, 8 - size / 2));
  61.                 if (lightness > 1)
  62.                         lightness -= 0.0025;
  63.                 for (int i = 0; i < quantity; ++i)
  64.                 {
  65.                         if (success_p > creat_random(0, 100) / 100.0)
  66.                         {
  67.                                 COLORREF color = colors[creat_random(0, 6)];
  68.                                 points[index].color = RGB(GetRValue(color) / lightness, GetGValue(color) / lightness, GetBValue(color) / lightness);
  69.                                 points[index].x = size * origin_points[i].x + creat_random(-4, 4);
  70.                                 points[index++].y = size * origin_points[i].y + creat_random(-4, 4);
  71.                         }
  72.                 }
  73.         }
  74.         int points_size = index;
  75.         for (int frame = 0; frame < frames; ++frame)
  76.         {
  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 diatance_increase = -0.0009 * distance * distance + 0.35714 * distance + 5;
  84.                         double x_increase = diatance_increase * x / distance / frames;
  85.                         double y_increase = diatance_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.                 {
  93.                         for (index = 0; index < quantity; ++index)
  94.                         {
  95.                                 if ((creat_random(0, 100) / 100.0 > 0.6 && size >= 20) || (size < 20 && creat_random(0, 100) / 100.0>0.95))
  96.                                 {
  97.                                         double x, y;
  98.                                         if (size >= 20)
  99.                                         {
  100.                                                 x = origin_points[index].x * size + creat_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
  101.                                                 y = origin_points[index].y * size + creat_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
  102.                                         }
  103.                                         else
  104.                                         {
  105.                                                 x = origin_points[index].x * size + creat_random(-5, 5);
  106.                                                 y = origin_points[index].y * size + creat_random(-5, 5);
  107.                                         }
  108.                                         setfillcolor(colors[creat_random(0, 6)]);
  109.                                         solidcircle(screen_x(x), screen_y(y), 1);
  110.                                 }
  111.                         }
  112.                 }
  113.         }
  114.         SetWorkingImage();
  115. }
  116. int main()
  117. {
  118.         initgraph(xScreen, yScreen);
  119.         BeginBatchDraw();
  120.         srand(time(0));
  121.         creat_data();
  122.         bool extend = true, shrink = false;
  123.         for (int frame = 0; !_kbhit();)
  124.         {
  125.                 putimage(0, 0, &images[frame]);
  126.                 FlushBatchDraw();
  127.                 Sleep(20);
  128.                 cleardevice();
  129.                 if (extend)
  130.                         frame == 19 ? (shrink = true, extend = false) : ++frame;
  131.                 else
  132.                         frame == 0 ? (shrink = false, extend = true) : --frame;
  133.         }
  134.         EndBatchDraw();
  135.         closegraph();
  136.         return 0;
  137. }
复制代码
EasyX插件

下载所在:https://easyx.cn/t/download

安装:双击exe文件,下一步,安装

假如安装乐成后,引用照旧报错的话,就去安装路径里找到graphics.h文件复制到项目文件夹中,然后在代码中利用#include "graphics.h"语句引用

项目源码

链接:
提取码: 4vqe

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表