当前位置:   article > 正文

用EasyX制作线性筛素数动画_easyx简单easyx动画

easyx简单easyx动画

这个代码完成了模拟线性筛的动画。效果如下:
在这里插入图片描述

代码如下:

#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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/94125
推荐阅读
相关标签
  

闽ICP备14008679号