这是一个非常简单实用的解决方案(C++):
#include <iostream>
#include <vector>
struct Run { int x, w; };
enum { EAST, NORTHEAST, NORTH, NORTHWEST, WEST, SOUTHWEST, SOUTH, SOUTHEAST };
int main() {
const Run data[] = {
{ 7, 2 },
{ 5, 6 },
{ 5, 7 },
{ 5, 7 },
{ 6, 6 },
{ 0, 12 },
{ 0, 12 },
{ 0, 11 },
{ 1, 7 },
{ 3, 4 },
{ 3, 4 },
{ 3, 5 },
{ 3, 7 },
{ 3, 7 },
{ 5, 5 }
};
std::vector<Run> runs(data, data + 15);
std::vector<int> before;
std::vector<int> after;
unsigned int i;
int j;
for (i = 0; i < runs.size() - 1; ++i) {
if (runs[i].x < runs[i + 1].x) {
for (j = 0; j < runs[i + 1].x - runs[i].x - 1; ++j)
before.push_back(WEST);
before.push_back(NORTHWEST);
} else if (runs[i].x > runs[i + 1].x) {
before.push_back(NORTHEAST);
for (j = 0; j < runs[i].x - runs[i + 1].x - 1; ++j)
before.push_back(EAST);
} else {
before.push_back(NORTH);
}
int first_right(runs[i].x + runs[i].w);
int second_right(runs[i + 1].x + runs[i + 1].w);
if (first_right < second_right) {
after.push_back(SOUTHEAST);
for (j = 0; j < second_right - first_right - 1; ++j)
after.push_back(EAST);
} else if (first_right > second_right) {
for (j = 0; j < first_right - second_right - 1; ++j)
after.push_back(WEST);
after.push_back(SOUTHWEST);
} else {
after.push_back(SOUTH);
}
}
for (j = 0; j < runs.back().w - 1; ++j)
after.push_back(WEST);
std::reverse(before.begin(), before.end());
after.insert(after.end(), before.begin(), before.end());
for (j = 0; j < int(after.size()); ++j) {
switch (after[j]) {
case EAST: std::cout << "EAST\n"; break;
case NORTHEAST: std::cout << "NORTHEAST\n"; break;
case NORTH: std::cout << "NORTH\n"; break;
case NORTHWEST: std::cout << "NORTHWEST\n"; break;
case WEST: std::cout << "WEST\n"; break;
case SOUTHWEST: std::cout << "SOUTHWEST\n"; break;
case SOUTH: std::cout << "SOUTH\n"; break;
case SOUTHEAST: std::cout << "SOUTHEAST\n"; break;
}
}
}
这通过迭代运行,测试左右端点的跳转方向,并将适当数量的链元素添加到两个向量:一个正向,右侧,一个反向顺序, 对于左边。然后它通过为最后一条扫描线添加适当数量的链接来连接两条链,然后反转左侧链并将其附加到右侧以生成最终链。
希望这就是你要找的!