获取二维矩阵的相邻元素(仅限深度一)

发布于 2024-10-28 12:43:26 字数 491 浏览 8 评论 0原文

我有一张 800 x 600 的图像。我想将它视为一个矩阵并获取相邻元素

例如。

(0,0) (1,0) (2,0) (3,0)

(0,1) (1,1) (2,1) (3,1)

(0,2) (1,2) (2,2) (3,2)

(0,3) (1,3) (2,3) (3,3)

示例解: (0,0) 邻近于: (1,0) (0,1) (1,1)

(1,1) 邻近于: (0,0) (1,0) (2,0) (2 ,1) (2,2) (1,2) (0,2) (0,1)

所以我写了一个结构体数组,我将把这些点中的每一个存储到其中,

typdef struct point
{
    int x;
    int y;
}point[800*600];

所以我的第一个想法是实现一个 dfs 但是但这并没有真正奏效,所以我想获得外部意见,以使自己保持在正确的轨道上。谢谢

I have an 800 by 600 image. I want to treat it like a matrix and get the adjacent elements

ex.

(0,0) (1,0) (2,0) (3,0)

(0,1) (1,1) (2,1) (3,1)

(0,2) (1,2) (2,2) (3,2)

(0,3) (1,3) (2,3) (3,3)

example solutions:
(0,0) is adjacent to: (1,0) (0,1) (1,1)

(1,1) is adjacent to: (0,0) (1,0) (2,0) (2,1) (2,2) (1,2) (0,2) (0,1)

so I have written a struct array that i will store each one of these points into

typdef struct point
{
    int x;
    int y;
}point[800*600];

so my first idea was to implement a dfs but that did not really work out so I wanted to get an outside opinion to keep myself on the right track. thanks

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

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

发布评论

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

评论(1

成熟的代价 2024-11-04 12:43:26

最终答案取决于您如何将 2D 显示的排列可视化为 1D 数组(“行在先,列在最后”或“列在先,行在最后”)。

假设“行优先”(沿行的像素增量为 1,沿列的像素增量为 ROW_LENGTH):

首先使用一些定义来设置 ROW_LENGTH 和 COL_LENGTH 的值

#define ROW_LENGTH 800
#define COL_LENGTH 600

然后,如果需要更改,您可以轻松调整大小,而不会影响其余部分的大小代码有效。

typdef struct point
{
    int x;
    int y;
}point[ROW_LENGTH*COL_LENGTH];

稍后,可以通过以下方式获得相邻点:

adjacentWest = point[(y*ROW_LENGTH) + (x-1)];
adjacentNorth = point[((y+1)*ROW_LENGTH) + x];

您需要将北和南调整为+1或-1,具体取决于您的原点是在显示屏的左上角还是左下角。

The final answer depends on how you visualize the arrangement of your 2D display into the 1D array ('row-first column-last' or 'column-first row-last').

Assuming 'row-first' (pixels along the row increment by 1, pixels along the column increment by ROW_LENGTH):

First use some definitions to set values for ROW_LENGTH and COL_LENGTH

#define ROW_LENGTH 800
#define COL_LENGTH 600

Then you can easily adjust your size if needs change without affecting how the rest of the code works.

typdef struct point
{
    int x;
    int y;
}point[ROW_LENGTH*COL_LENGTH];

Later, adjacent points can be obtained with something like:

adjacentWest = point[(y*ROW_LENGTH) + (x-1)];
adjacentNorth = point[((y+1)*ROW_LENGTH) + x];

You'll need to adjust North and South to be +1 or -1 depending on if your origin is in the top-left or bottom-left of your display.

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