简体   繁体   English

C ++在2D数组中找到两点之间的距离

[英]C++ finding distance between two points in 2D array

I'm working with inheritance right now, which I'm having no problem with. 我现在正在使用继承,我没有问题。 I have this vector of strings (basically a 2D array of char's). 我有这个字符串向量(基本上是char的2D数组)。

The exact middle is where the shark is and the character of the shark changes based on where the other fish are. 确切的中间是鲨鱼的位置,鲨鱼的性格根据其他鱼的位置而变化。

For example 例如

.....
.....
^.l..
.....
.....

The arrows are where the fish are and since the fish is to the left. 箭头是鱼的位置,因为鱼是左边的。 The shark becomes a 'l' 鲨鱼变成'l'

.>...
.....
..u..
.....
.....

The arrows are where the fish are and since the fish is up. 箭头是鱼的位置,因为鱼是向上的。 The shark becomes a 'u' 鲨鱼变成'你'

The code I had to do this correctly is this: 我必须正确执行此操作的代码是:

void Shark::point(std::vector<std::string>& map){

if (map[0][0] != '.' || map[1][0] != '.' || map[2][0] != '.' || map[3][0] != '.' || map[4][0] != '.' || map[1][1] != '.' || map[2][1] != '.' || map[3][1] != '.'){
ProtoFish::m_direction = Direction::left;
}
if (map[0][1] != '.' || map[0][2] != '.' || map[0][3] != '.' || map[1][2] != '.'){
ProtoFish::m_direction = Direction::up;
}
if (map[4][1] != '.' || map[4][2] != '.' || map[4][3] != '.' || map[3][2] != '.'){
ProtoFish::m_direction = Direction::down;
}
if (map[0][4] != '.' || map[1][4] != '.' || map[2][4] != '.' || map[3][4] != '.' || map[4][4] != '.' || map[1][3] != '.' || map[2][3] != '.' || map[3][3] != '.'){
ProtoFish::m_direction = Direction::right;
}
}

I am basically just checking if the fish are in this map: 我基本上只是检查鱼是否在这张地图中:

    l u u u r
    l l u r r
    l l . r r
    l l d r r
    l d d d r

where if the '.' 如果是'。' char doesn't exist that's where a fish is, so the Shark should point in that direction. char不存在于鱼的位置,所以鲨鱼应指向那个方向。

The problem occurs where there are multiple fish. 问题发生在有多条鱼的地方。

>....
..>..
..*..
.>...
..^>.

I understand what I'm supposed to do, but I have no idea of how to do it, or how to even get started. 我理解我应该做什么,但我不知道该怎么做,或者如何开始。 The goal is just to get the Shark facing the same direction of the nearest fish. 目标只是让鲨鱼面向最近鱼的方向。 So basically the arrow "closest" to the middle. 所以基本上箭头“最接近”中间。 Can someone help me out to get started with this? 有人可以帮助我开始这个吗? Maybe like a separate function that calculates the distance of each element from the center? 也许是一个单独的函数来计算每个元素与中心的距离?

You need to calculate a distance for each fish - to do this you would convert your fish position to an (x,y) coordinate. 您需要计算每条鱼的距离 - 为此,您可以将鱼的位置转换为(x,y)坐标。 Where the shark is is the (0,0) coordinate. 鲨鱼的位置是(0,0)坐标。

.....
.....
^.l..
.....
.....

would be (-2,0) for the fish ^

.>...
.....
..u..
.....
.....

would be (-1,3) for the fish >

so this coordinate conversion from 2D is easy to do. 所以这种从2D的坐标转换很容易。 For the distance you use pythagoros theorem in 2 dimension-in this case its just square root of the square of x coordinate+square of y coordinate. 对于距离,你在二维中使用pythagoros定理 - 在这种情况下,它只是x坐标的平方的正方根+ y坐标的平方。 You then need another new array to store the distances for each fish. 然后,您需要另一个新阵列来存储每条鱼的距离。 Then you need to search that new distance array for smallest value where the index of the array is a fish index (like the id of fish). 然后你需要搜索新的距离数组以获得最小值,其中数组的索引是鱼索引(如鱼的id)。 This will give you the nearest fish to the shark thus you can get get the shark facing the right way if there is one fist. 这将为您提供离鲨鱼最近的鱼类,因此如果有一个拳头,您可以让鲨鱼面向正确的方向。 However if there is more than one fish with the same distance, you need to think of a new rule in this case for example (could it be if there are more fish to left than right and the left and right fish are the same distance away-then face left beacause there is more fish?-or shark food-is a suggestion). 但是如果有不止一条距离相同的鱼,你需要在这种情况下考虑一个新的规则(例如,如果剩下的鱼比右边多,左右鱼距离相同的距离 - 然后面对左边因为有更多的鱼? - 或鲨鱼食物 - 是一个建议)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM