对我来说,我有一个非常基本的需求:提取构成矩阵的区域的坐标。
让我举个例子。这是一些矩阵:
| A | B | D | E | F | G | H | I | J |
| 1 | 0 | 0 | 0 | 2 | 2 | 2 | 4 | 4 | 4 |
| 2 | 0 | 0 | 2 | 2 | 2 | 2 | 4 | 4 | 4 |
| 3 | 0 | 0 | 2 | 2 | 2 | 3 | 3 | 4 | 4 |
| 4 | 1 | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 |
| 5 | 1 | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 |
| 6 | 1 | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 |
| 7 | 1 | 0 | 0 | 0 | 1 | 1 | 3 | 0 | 4 |
| 8 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
| 9 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 |
| 10| 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 |
我希望得到一个包含每个区域的边界和值的数组(不需要特定的顺序)。
左上角区域的示例:
您是否知道有些图书馆会回答这个问题,或者我应该自己编写代码吗?
答案 0 :(得分:1)
我自己编写代码,不确定是否有库。
我会依次考虑每个地区的每个点。然后(我认为)这应该有效:
if (surrounding 8 squares has at least one with different region)
{
for each 3 squares, above, below, left and right
{
if (less than 3 are different, and the middle is different)
{
is a boundry
}
}
for each 3 squares, above, below
{
for each 3 squares, left, right
{
if(all 3 from outer loop and all 3 from inner loop are different)
{
is a boundry
}
}
}
not a boundry
}
else
{
not a boundry
}
将越界方块视为不同。