EasyX测试布局代码

在这里插入图片描述

#include <iostream>
#include <algorithm>
#include <graphics.h>		// 引用图形库头文件
#include <conio.h>
#include <unordered_map>
#include <Windows.h>
#include "layout/LayoutSystem.h"

#define DEFAULT_PANELS_LAYOUT_PADDING 0.03
#define DEFAULT_PANELS_LAYOUT_CELL_SIZE 0.01

using namespace boxing::layout;

int SCRW = 640;
int SCRH = 480;
int main()
{
    HWND hConsole = GetConsoleWindow();

	initgraph(SCRW, SCRH);	// 创建绘图窗口,大小为 640x480 像素
    // 设置控制台窗口不最小化
    SetWindowPos(hConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);

    auto getX = [](float x1)->int {return (int)((x1 + 1.0) / 2.0 * SCRW); };
    auto getY = [](float y1)->int {return (int)(abs(y1 - 1.0) / 2.0 * SCRH); };
    auto getW = [](float w)->int {return  w / 2.0 * SCRW; };
    auto getH = [](float h)->int {return  h / 2.0 * SCRH; };
    auto drawRectText = [&](float x,float y, LPCTSTR str,float w,float h, COLORREF color) {
        int new_x = getX(x);
        int new_y = getY(y);
        int new_w = getW(w);
        int new_h = getH(h);
        setfillcolor( color);
        fillrectangle( new_x- new_w/2, new_y- new_h/2, new_x + new_w / 2, new_y + new_h / 2 );
        int textW = textwidth(str);
        int textH = textheight(str);
        setfillcolor(RGB(255,255,255));
        outtextxy(new_x - textW / 2.0, new_y - textH / 2.0,str );
    };

    auto drawRectCharText = [&](float x, float y, char* str, float w, float h, COLORREF color) {
        int num = MultiByteToWideChar(0, 0, str, -1, NULL, 0);
        wchar_t* wide = new wchar_t[num];
        MultiByteToWideChar(0, 0, str, -1, wide, num);
        drawRectText(x, y, wide, w, h, color);
        free(wide);
    };

    auto drawRectInt = [&](float x, float y, int text, float w, float h, COLORREF color) {
        char str[50] = { 0 };
        sprintf(str, "%d", text);
        drawRectCharText(x, y, str, w, h, color);
    };
    auto drawRectFloat = [&](float x, float y, float text, float w, float h, COLORREF color) {
        char str[50] = { 0 };
        sprintf(str, "%f", text);
        drawRectCharText(x, y, str, w, h, color);
    };

    auto drawLine = [&](float start_x, float start_y, float end_x, float end_y, COLORREF color) {
        setlinecolor(color);
        line(getX(start_x), getY(start_y), getX(end_x), getY(end_y));
    };
    srand(time(NULL));
    bool loop = false;
    for (int count = 100; count >0; count--) {
        cleardevice();
        float aspect = 1.0 / 0.618; // w/h
        int col = min(count, (int)std::ceil(sqrt(count * aspect)));
        int row = std::ceil((float)count / col);

        std::unordered_map<int,float> widthInCol;
        std::unordered_map<int,float> heightInRow;

        int rowMin = row / 2 - (count - 1) / col;
        int rowMax = row / 2 - ((int)0) / col;
        int colMax = (col - 1) % col - col / 2;
        int colMin = ((int)0) % col - col / 2;
        float col_centre = (colMax + colMin) / 2.0;
        float row_centre = (rowMax + rowMin) / 2.0;
        drawLine(-1,0,1,0, RGB(0 ,255, 0));
        drawLine(0, 1, 0, -1, RGB(0, 255, 0));
        char str[100] = { 0 };
        sprintf(str, "r:%d,c:%d", row,col);
        drawRectCharText(-0.9,0.95, str, 0.2, 0.1, RGB(255, 250, 0));
        printf(" count: %d,colMax: %d,colMin:%d,rowMax:%d ,rowMin:%d, tt:%f,row_centre:%f\n", count, colMax, colMin, rowMax, rowMin, col_centre, row_centre);
        

        LayoutSystem::Ptr layoutSystem = LayoutSystem::NEW(PAGE_LAYOUT_PLANE, DEFAULT_PANELS_LAYOUT_CENTRE, DEFAULT_PANEL_POSITION);
        for (int index = 0; index < count; index++) {
         /*   int tmpIndex = count - 1 - index;
            int prow = row / 2 - tmpIndex / col;
            int pcol = tmpIndex % col - col / 2;
            int prow_new = abs(prow - rowMax);
            prow_new = prow_new % 2 == 0 ? (prow_new / 2) : ((prow_new + 1) / -2);
            int pcol_new = abs(pcol - colMin);
            pcol_new = pcol_new % 2 == 0 ? (pcol_new / 2) : ((pcol_new + 1) / -2);

            float element_row = prow_new + row_centre;
            float element_col = pcol_new - col_centre;
            printf(" count: %d,index: %d,row:%d, col:%d, [%d,%d],prow_new:%d,pcol_new:%d\n", count, index, row, col, prow, pcol, prow_new, pcol_new);

            int color = (float)tmpIndex / count * 255;*/

            float randW = max(0.1,rand() / double(RAND_MAX)/5.0);
            float randH = max(0.1, rand() / double(RAND_MAX) / 5.0);
            layoutSystem->addLayoutElement(index, randW, randH);
     /*       if (widthInCol.count(pcol_new) > 0) {
                widthInCol[pcol_new] += randW;
            }else {
                widthInCol[pcol_new] = randW;
            }
            if (heightInRow.count(prow_new) > 0) {
                heightInRow[prow_new] += randH;
            }else {
                heightInRow[prow_new] = randH;
            }*/
            //drawRectInt(element_col /(max(abs(colMax), abs(colMin))+1), element_row / (max(abs(rowMax), abs(rowMin)) + 1), index, randW, randH, RGB(255, color, 0));
        }
        layoutSystem->startCompositor(false);
        auto layout_results = layoutSystem->getLayoutResults();
        for (auto [k, v] : layout_results) {
            int color = (float)k / count * 255;
            //drawRectInt(v->position.x / (max(abs(colMax), abs(colMin)) + 1), v->position.y / (max(abs(rowMax), abs(rowMin)) + 1), k, 0.1, 0.1, RGB(255, color, 0));
            auto element = layoutSystem->getLayoutElement(k);
            drawRectInt(v->position.x, v->position.y , k, element->mWidth, element->mHeight, RGB(255, color, 0));
        }

        //auto tmpC = std::max_element(widthInCol.begin(), widthInCol.end(), [](std::pair<int, float> left, std::pair<int, float> right) { return left.second < right.second; });
        //float maxWidth = tmpC->second;
        //auto tmpR = std::max_element(heightInRow.begin(), heightInRow.end(), [](std::pair<int, float> left, std::pair<int, float> right) { return left.second < right.second; });
        //float maxHeight = tmpR->second;



        //drawRectFloat(-0.5, 0.95, maxWidth, 0.2, 0.1, RGB(25, 25, 250));
        //drawRectFloat(0, 0.95, maxHeight, 0.2, 0.1, RGB(25, 25, 250));
        Sleep(1000);
        if (!loop)
            break;
    }
	_getch();				
	closegraph();			
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值