C/C++教程

切割矩形,C++版

本文主要是介绍切割矩形,C++版,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

#include <iostream>
#include <vector>

// 点
class Node
{
public:
    int x;
    int y;
};
// 保存每个块的左上角和右下角的坐标
class BlockData {
public:
    Node lp;
    Node rp;
};
// 分割的数据
class SpliceData
{
public:
    int cnt; // 块数
    int rowCnt;// 行快高
    int colCnt; // 列块高
    std::vector<BlockData> blocks; // 块数据,保存每个块的左上角和右下角的坐标
};

SpliceData slice_rectangle(int width, int height, int span) {
    SpliceData spliceData;
    int lx, ly, bx, by;
    lx=ly=bx=by=0;
    while (by < height) {
        by = ly + span;
        if (by > height) {
            by = height;
        }
        lx = bx = 0;
        spliceData.rowCnt++;
        spliceData.colCnt = 0;
        while (bx < width) {
            spliceData.colCnt++;
            bx = lx + span;
            if (bx > width) {
                bx = width;
            }
            BlockData blockData;
            blockData.lp.x = lx;
            blockData.lp.y = ly;
            blockData.rp.x = bx;
            blockData.rp.y = by;
            spliceData.blocks.push_back(blockData);
            lx = bx;
        }
        ly = by;
    }
    return spliceData;
}

int main()
{
    // 测切块
    DWORD t1, t2;
    t1 = GetTickCount();
    SpliceData spliceData = slice_rectangle(1920, 1080, 30);
    t2 = GetTickCount();
    std::cout << "spliceData Time:" << (t2 - t1) * 1.0 / 1000 << "\n";
    std::cout << spliceData.rowCnt << "x" << spliceData.colCnt << std::endl;
    /*for (int i = 0; i < spliceData.blocks.size();i++) {
        std::cout << "left node: " << spliceData.blocks[i].lp.x << " " << spliceData.blocks[i].lp.y << std::endl;
        std::cout << "right node: " << spliceData.blocks[i].rp.x << " " << spliceData.blocks[i].rp.y << std::endl;
        std::cout << std::endl;
    }*/
    system("PAUSE ");
}

 

这篇关于切割矩形,C++版的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!