在c中创建一个网格
我需要在 c 中创建一个 375 x 375 网格,并且需要创建一个由 25 (5x5) 个极点组成的网格,这些极点之间的间隔为 75 (375/5) 个网格点。然后,我需要计算任何给定的网格点与极点的距离。这不是一个可视网格,但我仍然无法理解如何以编程方式创建此网格设置。到目前为止,我已经创建了一个由两个整数 x 和 y 组成的“Point”结构:
typedef struct{
int x, y;
}Point;
然后我将网格定义为 375x375 点数组,并将 x 和 y 值设置为 i
和 j
恭敬地,在嵌套的 for 循环中。然而,我很难弄清楚如何创建杆位数组。它应该是一个 5x5 数组,其中 x 和 y 值设置为 i*15
和 j*15
吗?如果是这样,那么我将如何一起使用两个 2D 数组。显然我无法进行这样的计算:
for(i = 0; i < 375; i ++){
for(j = 0; j<375; j++){
distance(polePos[i][j], grid[i][j]);
}
}
但是如果我将其设为 375x375 数组,那么我如何指定极点位置? 我走在正确的轨道上吗?任何帮助将不胜感激。
I need to create a 375 x 375 grid in c, and I need to create a grid of 25 (5x5) poles that will be spaced 75 (375/5) grid points apart. I will then need to calculate for any given grid point how close it is to a pole. This is not a visual grid, but I'm still having trouble wrapping my head on how to create this grid set-up programmatically. So far, I'v created a "Point" struct that consist of two ints x and y:
typedef struct{
int x, y;
}Point;
I then define my grid as a 375x375 Point array and set the x and y values asi
and j
respectfully, in a nested for loop. However I'm having a hard time figuring out how to create the pole position array. Should it be a 5x5 array with the x and y values set as i*15
andj*15
? If so then how would I use the two 2D arrays together.I couldn't do a calculation like this obviously:
for(i = 0; i < 375; i ++){
for(j = 0; j<375; j++){
distance(polePos[i][j], grid[i][j]);
}
}
but If I make it a 375x375 array, then how do I designate the pole position?
Am I on the right track? Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有指定极点应放置在哪个列/行索引处,因此在下面我将它们从 0 开始间隔 75 放置,因此极点位于列/行索引 0、75、150、225 处、 300、 375(因此 6 极)。它可能不完全是您的想法,但它会帮助您开始。
You didn't specify at which column-/row-indices the poles should be placed so in the following i place them 75-apart starting at 0, so the poles are at column-/row-indices 0, 75, 150, 225, 300, 375 (so 6 poles). It might not be precisely what you had in mind but it will get you started.
如果您有两个网格
polePos[5][5]
和grid[375][375]
点,您可以使用以下算法计算距离:有
pI
和pJ
包含距离当前网格点最近的极点。If you have two grids
polePos[5][5]
andgrid[375][375]
of points you can compute the distances with the following algorithm:There
pI
andpJ
contain the nearest pole to the current grid point.如果您的网格和您的极相关,也许最好将它们放在同一结构中。
你可以把它看作一个棋盘,每个棋子上可能有棋子,也可能没有棋子。
您可以使用简单或经典的算法,例如 Dijkstra、贝尔曼-福特,A* 以确定到达一根杆子的最短路径。
If your grid and your pole are related, maybe it should be better to put them in the same structure.
You can see it like a chess-board, which may have or have not a piece on each case.
You can after use naive or classical algorithms like Dijkstra, Bellman-Ford, A* in order to determine shortest path to one of your pole.