检查数字高度图中的闭合路径

发布于 2024-09-24 18:49:53 字数 474 浏览 2 评论 0原文

我正在开发一个游戏,我需要检查给定数字高度图中的闭合路径: 服务器和客户端使用此高度图来设置正确的坐标以进行移动等... 现在,当用户走在“特殊”瓷砖上时,它会亮起...... 我的问题是: 当用户在这些图块上行走时,会创建一条包含空图块的闭合路径,服务器应自动填充此路径中的图块...

它应该执行如下操作: http://www.youtube.com/watch?v=kAVUNE2NTUQ - 1:32

我确信我必须在这里或那里使用一些数学,但我不知道如何...... 我可以做一个“for”循环,但它太长了,而且问题是服务器需要在用户每次行走时执行循环...... 预先感谢您的回答,希望有人能帮助我。

PS:我正在使用 C#

编辑:当用户在图块上行走时,服务器会自动将高度图 [X, Y] 替换为代表用户颜色的整数

I'm working on a game, and I have the necessity to check a closed path in a given numerical heightmap:
The server and the client use this heightmap to set the right coords to move etc...
Now, when an user walks on a "special" tile, it lights...
My problem is:
When an user, walking on these tiles creates a closed path with empty tiles in it, the server should automatically fill the tiles in this path...

It should do something like this:
http://www.youtube.com/watch?v=kAVUNE2NTUQ - 1:32

I'm sure I've to use some maths here or there, but I dunno how...
I could do a "for" cycle, but it would be too long, and the problem is that the server needs to do the cycle every time an user walks...
Thanks in advance for your answers, hope someone could help me.

PS: I'm using C#

EDIT: When an user walks on a tile, the server automatically replaces the heightmap[X, Y] with an integer that represents the color of the user

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

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

发布评论

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

评论(3

天荒地未老 2024-10-01 18:49:53

该问题可以分为两个部分:

  1. 检测路径何时关闭。实现此目的的一种方法是在路径中的连续图块之间建立链接:当您迈出一步时,如果之前访问过您的一个新邻居,并且您无法在当前邻居中追踪到该新邻居的路径 em> 那么你已经关闭了路径(你可能需要建立额外的链接来处理沿着你的路径返回的情况)。这也将告诉您内部区域位于路径的哪一侧(左或右)。在方格纸上尝试一下,它应该会变得清晰。每一步的复杂度都是 O(1)。
  2. 填充封闭区域。一旦找到该区域中的单个图块,就迭代它的邻居,然后迭代它们的邻居,依此类推。对于面积为 n 的区域,时间复杂度为 O(n),内存平均复杂度为 O(sqrt(n)),具体取决于几何结构(最坏情况下为 O(n))。

The problem can be divided into two parts:

  1. Detecting when a path has closed. One way to do this is by making links between sequential tiles in the path: when you take a step, if one of your new neighbors has been visited before and you cannot trace your path back to it among your current neighbors then you have closed the path (you may have to establish additional links to deal with the case of going back alongside your path). This will also tell on which side of your path (left or right) the interior region lies. Play around with this on graph paper and it should become clear. This is O(1) with each step.
  2. Filling in the enclosed region. Once you have found a single tile that is in the region, iterate over its neighbors, and then their neighbors, and so on. For a region of area n, this is O(n) in time and on average O(sqrt(n)) in memory, depending on geometry (O(n) at worst).
末蓝 2024-10-01 18:49:53

假设您有一个矩形区域,左上角位于 (0, 0),右下角位于 (M, N)。然后可以使用以下伪代码填充算法:

find the upper and bottom cells of the path (yTop, yBottom)

for (int y = yTop; y != yBottom; ++y)
{
    find leftmost and rightmost path cells (xLeft, xRight) for that y
    bool isInside = true;
    for (int x = xLeft+1; x<xRight; ++x)
    {
        if ((x, y) is path cell)
            isInside = !isInside;
        if (isInside)
            fill(x, y);
    }
}

Let's assume you have rectangular field with upper left corner in (0, 0) and bottom right in (M, N). Then you can use the following pseudocode fill algorithm:

find the upper and bottom cells of the path (yTop, yBottom)

for (int y = yTop; y != yBottom; ++y)
{
    find leftmost and rightmost path cells (xLeft, xRight) for that y
    bool isInside = true;
    for (int x = xLeft+1; x<xRight; ++x)
    {
        if ((x, y) is path cell)
            isInside = !isInside;
        if (isInside)
            fill(x, y);
    }
}
皓月长歌 2024-10-01 18:49:53

“问题是服务器每次用户行走时都需要执行循环”...

您可以通过为游戏中的每个图块分配一个唯一的整数、具有包含图块数量的数组,以及然后对于每个步骤,在数组中标记此图块。当然,还要检查该图块之前是否已经走过(即是否在数组中标记),如果有,则有一个循环(可能是零面积)。这种方法没有循环遍历已走的图块等,而只是对每个步骤进行一次查找。

"the problem is that the server needs to do the cycle every time an user walks"...

You can eliminate the cycling over walked tiles by assigning each tile in the game a unique integer, having an array with the number of tiles, and then for each step mark this tile in the array. Of course, also check whether the tile has been walked before (i.e. whether it's marked in the array), and if it has, you have a loop (maybe of zero area). This approach has no cycling over walked tiles, etc, but just a single look-up for each step.

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