切割矩形,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 "); }