使用EasyX为线性筛创造动画

更好的阅读体验:http://t.csdn.cn/pvMNR


代码如下:

#include <iostream> #include <string> #include <graphics.h> using namespace std; #define BLOCK_WIDTH 70 #define START_X 80 #define START_Y 60 #define MAX_PER_LINE 10 #define TEXT_COLOR_UNCHOOSE RGB(0, 0, 0) #define BOADER_COLOR_UNCHOOSE RGB(0, 0, 0) #define TEXT_COLOR_CHOOSE RGB(232, 16, 16) #define BOADER_COLOR_CHOOSE RGB(23, 213, 107) #define TEXT_COLOR_CHOOSE_2 RGB(220, 220, 157) #define BOADER_COLOR_CHOOSE_2 RGB(86, 153, 175) // 演示延迟时间 #define DELAY_TIME 1000 int textx = 850, texty = 100; // 输出运行消息 void printtext(LPCTSTR str) { // 清空 if (texty >= 700) { setbkcolor(WHITE); clearrectangle(850, 0, 1200, 1000); texty = 100; textx = 850; } settextstyle(22, 0, _T("Consolas")); settextcolor(BLACK); setbkcolor(WHITE); outtextxy(textx, texty, str); texty += textheight(str) + 3; } // 使文字居中对齐。使用了https://blog.csdn.net/qq_43655831/article/details/114987157的代码 void TextInRect(LPCTSTR str, int x, int y, int width, int height, bool horizontal = true, bool vertical = true) { LOGFONT font; gettextstyle(&font); int textHeight = font.lfHeight; int textWidth = textHeight; int strWidth = 0; int strNum = lstrlen(str); for (int i = 0; i < strNum; ++i) strWidth += (str[i] > 127) ? textHeight : textHeight / 2; if (strWidth >= width || textHeight >= height) { outtextxy(x, y, str); return; } if (horizontal) x += (width - strWidth) / 2; if (vertical) y += (height - textHeight) / 2; outtextxy(x, y, str); } // 对每一个小方块 class block { public: int var = 0; int x = 0; int y = 0; int width = BLOCK_WIDTH; int isChoose = 0; int markTime = 0; // 被标记的次数:用来展示线性筛On的特点 void draw() { FlushBatchDraw(); settextstyle(36, 0, _T("Consolas")); if (!isChoose) { setlinecolor(BOADER_COLOR_UNCHOOSE); settextcolor(TEXT_COLOR_UNCHOOSE); rectangle(this->x, this->y, this->x + BLOCK_WIDTH, this->y + BLOCK_WIDTH); } else if(isChoose == 1) { setbkcolor(BOADER_COLOR_CHOOSE); setfillcolor(BOADER_COLOR_CHOOSE); setlinecolor(BOADER_COLOR_CHOOSE); settextcolor(TEXT_COLOR_CHOOSE); fillrectangle(this->x, this->y, this->x + BLOCK_WIDTH, this->y + BLOCK_WIDTH); } else if (isChoose == 2) { setbkcolor(BOADER_COLOR_CHOOSE_2); setfillcolor(BOADER_COLOR_CHOOSE_2); setlinecolor(BOADER_COLOR_CHOOSE_2); settextcolor(TEXT_COLOR_CHOOSE_2); fillrectangle(this->x, this->y, this->x + BLOCK_WIDTH, this->y + BLOCK_WIDTH); } TextInRect(to_wstring(var).c_str(), x, y, BLOCK_WIDTH, BLOCK_WIDTH); setbkcolor(WHITE); } }; block blocks[100]; // 线性筛:代码来自OIwiki int cnt; int prime[50000], st[50000]; void Euler(int n) { cnt = 0; memset(prime, 0, sizeof(prime)); memset(st, 0, sizeof(st)); for (int i = 2; i <= n; ++i) { Sleep(DELAY_TIME); if (!st[i]) { wstring msg = _T("选择") + to_wstring(i) + _T("为质数"); printtext(msg.c_str()); prime[cnt++] = i; blocks[i - 2].isChoose = 1; blocks[i - 2].draw(); } for (int j = 0; i * prime[j] <= n; ++j) { Sleep(DELAY_TIME); st[i * prime[j]] = 1; blocks[i * prime[j] - 2].markTime++; blocks[i * prime[j] - 2].isChoose = 2; blocks[i * prime[j] - 2].draw(); wstring msg = to_wstring(i) + _T(" × ") + to_wstring(prime[j]) + _T(" = ") + to_wstring(i * prime[j]); printtext(msg.c_str()); msg = _T("标记") + to_wstring(i * prime[j]) + _T(",它现在被标记了") + to_wstring(blocks[i * prime[j] - 2].markTime) + _T("次"); printtext(msg.c_str()); if (i % prime[j] == 0) { msg = _T("而此时") + to_wstring(i) + _T(" % ") + to_wstring(prime[j]) + _T(" == 0,退出"); printtext(msg.c_str()); Sleep(DELAY_TIME); break; } } } printtext(_T("END")); } // 初始化 void init() { setbkcolor(WHITE); cleardevice(); settextstyle(36, 0, _T("Consolas")); for (int i = 0; i < MAX_PER_LINE; i++) { for (int j = 0; j < MAX_PER_LINE; j++) { int nowid = i * MAX_PER_LINE + j; blocks[nowid].var = 2 + nowid; blocks[nowid].x = j * BLOCK_WIDTH + START_X; blocks[nowid].y = i * BLOCK_WIDTH + START_Y; } } } void drawCode() { for (auto e : blocks) { e.draw(); } } int main() { initgraph(1500, 800); BeginBatchDraw(); init(); drawCode(); Euler(101); EndBatchDraw(); getchar(); closegraph(); }

__EOF__

本文作者SDLTF
本文链接https://www.cnblogs.com/sdltf/p/17611279.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   SD!LTF  阅读(51)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示