在c中创建一个网格

发布于 2025-01-06 15:44:11 字数 692 浏览 1 评论 0原文

我需要在 c 中创建一个 375 x 375 网格,并且需要创建一个由 25 (5x5) 个极点组成的网格,这些极点之间的间隔为 75 (375/5) 个网格点。然后,我需要计算任何给定的网格点与极点的距离。这不是一个可视网格,但我仍然无法理解如何以编程方式创建此网格设置。到目前为止,我已经创建了一个由两个整数 x 和 y 组成的“Point”结构:

typedef struct{
    int x, y;
}Point;

然后我将网格定义为 375x375 点数组,并将 x 和 y 值设置为 ij恭敬地,在嵌套的 for 循环中。然而,我很难弄清楚如何创建杆位数组。它应该是一个 5x5 数组,其中 x 和 y 值设置为 i*15j*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 jrespectfully, 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 技术交流群。

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

发布评论

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

评论(3

陌上青苔 2025-01-13 15:44:11

您没有指定极点应放置在哪个列/行索引处,因此在下面我将它们从 0 开始间隔 75 放置,因此极点位于列/行索引 0、75、150、225 处、 300、 375(因此 6 极)。它可能不完全是您的想法,但它会帮助您开始。

#include "stdio.h"
#include "math.h"
double distance(double x, double y, double grid) {
    x = fmod(x, grid);
    y = fmod(y, grid); // <- these modulos makes us treat every interval like the interval 0...grid
    double dx = grid/2 - fabs(x -grid/2);
    double dy = grid/2 - fabs(y -grid/2);    
    //printf("%3u %3u %f %f \n", x, y, dx, dy);
    return sqrt(dx*dx+dy*dy);
}
int main(void) {
    int i; int j;
    double grid[375][375];
      for(i = 0; i < 375; i ++){
        for(j = 0; j<375; j++){
            grid[i][j] = distance(i, j, 75);
            printf("%3u %3u %f\n", i, j, grid[i][j]);
        }
    }
}

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.

#include "stdio.h"
#include "math.h"
double distance(double x, double y, double grid) {
    x = fmod(x, grid);
    y = fmod(y, grid); // <- these modulos makes us treat every interval like the interval 0...grid
    double dx = grid/2 - fabs(x -grid/2);
    double dy = grid/2 - fabs(y -grid/2);    
    //printf("%3u %3u %f %f \n", x, y, dx, dy);
    return sqrt(dx*dx+dy*dy);
}
int main(void) {
    int i; int j;
    double grid[375][375];
      for(i = 0; i < 375; i ++){
        for(j = 0; j<375; j++){
            grid[i][j] = distance(i, j, 75);
            printf("%3u %3u %f\n", i, j, grid[i][j]);
        }
    }
}
゛时过境迁 2025-01-13 15:44:11

如果您有两个网格 polePos[5][5]grid[375][375] 点,您可以使用以下算法计算距离:

double dist[375][375];

int pI = 0, pJ = 0;
for (int gI = 0; gI < 375; gI++) {
    while (pI + 1 < 5 &&
           abs(polePos[pI][pJ].x - grid[gI][0].x) >
           abs(polePos[pI + 1][pJ].x - grid[gI][0].x) {
        pI += 1;
    }

    for (int gJ = 0; gJ < 375; gJ++) {
        while (pJ + 1 < 5 &&
               abs(polePos[pI][pJ].y - grid[gI][gJ].y) >
               abs(polePos[pI][pJ + 1].y - grid[gI][gJ].y) {
            pJ += 1;
        }

        dist[gI][gJ] = distance(polePos[pI][pJ], grid[gI][gJ]);
    }

    pJ = 0;
}

pIpJ 包含距离当前网格点最近的极点。

If you have two grids polePos[5][5] and grid[375][375] of points you can compute the distances with the following algorithm:

double dist[375][375];

int pI = 0, pJ = 0;
for (int gI = 0; gI < 375; gI++) {
    while (pI + 1 < 5 &&
           abs(polePos[pI][pJ].x - grid[gI][0].x) >
           abs(polePos[pI + 1][pJ].x - grid[gI][0].x) {
        pI += 1;
    }

    for (int gJ = 0; gJ < 375; gJ++) {
        while (pJ + 1 < 5 &&
               abs(polePos[pI][pJ].y - grid[gI][gJ].y) >
               abs(polePos[pI][pJ + 1].y - grid[gI][gJ].y) {
            pJ += 1;
        }

        dist[gI][gJ] = distance(polePos[pI][pJ], grid[gI][gJ]);
    }

    pJ = 0;
}

There pI and pJ contain the nearest pole to the current grid point.

染墨丶若流云 2025-01-13 15:44:11

如果您的网格和您的相关,也许最好将它们放在同一结构中。

#include <stdbool.h>
typedef struct{
    int x, y;
    bool pole;
} Point;

你可以把它看作一个棋盘,每个棋子上可能有棋子,也可能没有棋子。

您可以使用简单或经典的算法,例如 Dijkstra贝尔曼-福特A* 以确定到达一根杆子的最短路径。

If your grid and your pole are related, maybe it should be better to put them in the same structure.

#include <stdbool.h>
typedef struct{
    int x, y;
    bool pole;
} Point;

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.

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