首页 > 其他分享 >使用EasyX为线性筛创造动画

使用EasyX为线性筛创造动画

时间:2023-08-07 14:13:10浏览次数:32  
标签:动画 WIDTH int EasyX COLOR CHOOSE 线性 BLOCK define

更好的阅读体验: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();
}

标签:动画,WIDTH,int,EasyX,COLOR,CHOOSE,线性,BLOCK,define
From: https://www.cnblogs.com/sdltf/p/17611279.html

相关文章

  • 线性基
    前线性基就相当于向量的基底,且表示的范围与原来表示的范围相同。插入线性基的过程本质上还是高斯消元,如果被消成全是\(0\)就说明这个向量能被其他向量线性表示。模板这里\(a_i\)表示第\(i\)位为\(1\),前面的全是\(0\)。voidin(llx){ for(inti=63;i>=0;i--) i......
  • 网络流与线性规划24题
    先贴个自己的Dinic板子。//最大流constintinf=0x3f3f3f3f3f3f3f3f;structEdge{ intfrom,to,cap; boolori; Edge(intu,intv,intc,boolo){ from=u,to=v,cap=c,ori=o; }};vector<Edge>edges;vector<int>id[10005];intadd_edge(intu,......
  • 线性同余方程
    Part1:前置知识扩展欧几里得算法(不会的点这里)Part2:求解线性同余方程1、定义给定整数\(a,b,m\),求一个整数\(x\)满足\(a*x\equivb\pmodm\),或者给出无解。因为未知数的指数为\(1\),所以我们称之为一次同余方程,也称线性同余方程。2、求解\(a*x\equivb\pmod......
  • 深度学习—线性回归预测销售额
    (深度学习—线性回归预测销售额)前提进行程序训练之前,需已经成功安装好深度学习环境若没有安装环境,可以参考:深度学习环境安装教程,进行环境安装。一、简介机器学习是人工智能的核心,是使计算机具有智能的根本途径。线性回归模型是机器学习中最简单、最基础的一类有监督学习模型......
  • 线性基(异或)
    线性基目录线性基定义:线性相关和线性无关基线性基的维护基本操作在线段树分治中的维护线性基的应用(代码模板在此)分析:代码:注:常用于异或定义:线性相关和线性无关平面向量基本定理:平面上两个不共线向量可以表示出该平面上任意一个向量这个定理可以拓展到n维有了这个,就能轻松理......
  • 线性基
    线性基用于解决异或相关的问题。如何构造线性基?设$p$为线性基的集合。插入一个数$x$时,枚举其最高位$i$,若$p_i$不存在,令$p_i=x$并退出,否则令$x=x:xor:p_x$。voidins(llx){ for(lli=SIZE-1;i>=0;i--) { if(!(x>>i))continue; if(!p......
  • 使用C#的窗体显示与隐藏动画效果方案 - 开源研究系列文章
    今天继续研究C#的WinForm的显示动画效果。上次我们实现了无边框窗体的显示动画效果(见博文:基于C#的无边框窗体动画效果的完美解决方案-开源研究系列文章),这次介绍的是未在任务栏托盘中窗体的显示隐藏动画效果的实现代码。1、项目目录;下面是项目目录,由基本的......
  • vue过度动画
    要过度的元素需要有v-if或者v-show,用transition包裹<transitionname="sort"><divclass="sort"v-show="show"><divclass="all-sort-list2"@click="goSearch">......
  • 6.数据分析(1) --描述性统计量和线性回归(1)
    ✅作者简介:热爱科研的算法开发者,Python、Matlab项目可交流、沟通、学习。......
  • 凸优化8——线性规划、二次规划
    线性规划以及等价变换中科大-凸优化笔记(lec25)-等价变换_凸优化等价_及时行樂_的博客-CSDN博客二次规划QP 二次约束二次规划QCQP中科大-凸优化笔记(lec26)-二次规划_二次约束二次规划_及时行樂_的博客-CSDN博客引入了lasso回归和岭回归......