如何计算数组中的其他元素

发布于 2024-08-31 10:40:02 字数 522 浏览 5 评论 0原文

基本上,我正在创建一个森林火灾程序,该程序取决于周围元素的风/干燥度。我有一个 var Trees [,] 数组,大小为 20 x 20。中间的方块设置为“着火”。单击按钮 1 后需要执行以下操作:评估着火方块周围的每个方块,以确定其他方块着火的概率。

Color[,] map = new Color[WIDTH, HEIGHT];
for (int x = 0; x < WIDTH; x++)
    for (int y = 0; y < HEIGHT; y++)
    {
        if (x == WIDTH / 2 && y == HEIGHT / 2)
            map[x, y] = Color.Red;
        else
            map[x, y] = Color.Green;
    }

        fireBox1.box = map;

这是我设置的 20 x 20 阵列,其中中间的方块着火。我只是不知道如何获取当前着火的方块周围的方块(数组元素)。

Basically I'm creating a forest fire program that depends on the wind / dryness of the surround elements. I have an array var Trees [,] that is 20 x 20. The middle square is set "on fire". This is what needs to be done once you click button1: Evaluate each square around the one that is set on fire to determine the probability for the others to catch fire.

Color[,] map = new Color[WIDTH, HEIGHT];
for (int x = 0; x < WIDTH; x++)
    for (int y = 0; y < HEIGHT; y++)
    {
        if (x == WIDTH / 2 && y == HEIGHT / 2)
            map[x, y] = Color.Red;
        else
            map[x, y] = Color.Green;
    }

        fireBox1.box = map;

This is the 20 x 20 array that I have setup with the middle square set on fire. I just have no idea how to get the squares (array elements) around the one that is currently on fire.

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

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

发布评论

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

评论(1

无法回应 2024-09-07 10:40:02

您可以从一个简单的循环开始。

for (int i = 0; i < 20; i++)
{
    for (int j = 0; j < 20; j++)
    {
        var tree = Trees[i, j];
        // ...
    }
}

建立矩阵后,中心应该如下所示。

[G][G][G]
[G][R][G]
[G][G][G]

然后我们可以只循环遍历与中心点相接触的点。

int centerX = 9;
int centerY = 9;
int beginX = centerX - 1; 
int endX = centerX + 1; 
int beginY = centerY - 1; 
int endY = centerY + 1; 

for (int y = beginY; y <= endY; y++)
{    
    for (int x = beginX ; x <= endX; x++)
    {
        //Skip the center
        if (x == centerX && y == centerY)
            continue;       
        // Calculate the chance of catching on fire.
        if (IsWindyPoint(x, y) || IsDryPoint(x, y))
            map[x, y] = Color.Yellow;
    }
}

因此,假设风向东吹,我们应该将其视为矩阵。

[G][G][G]
[G][R][Y]
[G][G][G]

最终它会像这样扩展。

[G][G][G][G]
[G][G][Y][Y]
[G][R][R][Y]
[G][G][Y][Y]
[G][G][G][G]

You can start with a simple loop.

for (int i = 0; i < 20; i++)
{
    for (int j = 0; j < 20; j++)
    {
        var tree = Trees[i, j];
        // ...
    }
}

After you have built your matrix the center should look like this.

[G][G][G]
[G][R][G]
[G][G][G]

Then we can loop through only the points that touch the center point.

int centerX = 9;
int centerY = 9;
int beginX = centerX - 1; 
int endX = centerX + 1; 
int beginY = centerY - 1; 
int endY = centerY + 1; 

for (int y = beginY; y <= endY; y++)
{    
    for (int x = beginX ; x <= endX; x++)
    {
        //Skip the center
        if (x == centerX && y == centerY)
            continue;       
        // Calculate the chance of catching on fire.
        if (IsWindyPoint(x, y) || IsDryPoint(x, y))
            map[x, y] = Color.Yellow;
    }
}

So assuming we have wind blowing east we should see this as the matrix.

[G][G][G]
[G][R][Y]
[G][G][G]

And eventually it will expand out like this.

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