获取二维矩阵的相邻元素(仅限深度一)
我有一张 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终答案取决于您如何将 2D 显示的排列可视化为 1D 数组(“行在先,列在最后”或“列在先,行在最后”)。
假设“行优先”(沿行的像素增量为 1,沿列的像素增量为 ROW_LENGTH):
首先使用一些定义来设置 ROW_LENGTH 和 COL_LENGTH 的值
然后,如果需要更改,您可以轻松调整大小,而不会影响其余部分的大小代码有效。
稍后,可以通过以下方式获得相邻点:
您需要将北和南调整为+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
Then you can easily adjust your size if needs change without affecting how the rest of the code works.
Later, adjacent points can be obtained with something like:
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.