从矩阵获取区域/区域边界

发布于 2024-12-17 06:45:25 字数 695 浏览 0 评论 0原文

对我来说,我有一个非常基本的需求:提取组成矩阵的区域的坐标。

让我举个例子。这是一些矩阵:

    | 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
  • 边界:{A1,D1,B2,B3,13}

您知道有一些库可以满足此需求还是我应该自己编写代码?

Sounds to me I've a very basic need: extracting coordinates of the zones composing a matrix.

Let me give an example. Here is some matrix:

    | 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 |

And I'd like to get an array with the boundaries and value of each zone (no particular order needed).

Example for the top-left zone:

  • value: 0
  • boundaries: {A1, D1, B2, B3, 13}

Do you know some library answering this need or should I code this myself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界和平 2024-12-24 06:45:25

我自己编写代码,不确定是否有库。

我会针对每个区域依次考虑每个点。那么(我认为)这应该可行:

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
}

将越界方块视为不同的。

I'd code this myself, not sure if there are libraries.

I'd consider each point in turn, for each region. Then (i think) this should work:

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
}

Treat out of bounds squares as different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文