活着的邻居细胞未正确计数
Alive neighbour cells not correctly counted
我知道我的标题不是很具体,但这是因为我不知道问题的来源。自2到3个小时以来,我一直遇到这个问题,从理论上讲,一切都应该起作用,但事实并非如此。这件代码:
for ( int x = -1; x <= 1; x++ ) { //Iterate through the 8 neighbour cells plus the one indicated
for ( int y = -1; y <= 1; y++ ) {
neighbour = coords(locX + x, locY + y, width); //Get the cell index in the array
if (existsInOrtho(ortho, neighbour)) { //If the index exists in the array
if (ortho[neighbour] == 0) { //Cell is dead
cnt--; //Remove one from the number of alive neighbour cells
}
} else { //Cell is not in the zone
cnt--; //Remove one from the number of alive neighbour cells
}
}
}
遍历所有邻居单元,以在数组中获取其值(1为活着,为0(。"坐标"功能,此处显示:
int coords(int locX, int locY, int width)
{
int res = -1;
locX = locX - 1; //Remove one from both coordinates, since an index starts at 0 (and the zone starts at (1;1) )
locY = locY - 1;
res = locX * width + locY; //Small calculation to get the index of the pixel in the array
return res;
}
在数组中获取单元格的索引。但是,当我运行代码时,它不起作用,邻居单元的数量不正确(就像每次附近的人都没有计算一个单元格(。我尝试手动分解所有内容,并且它有效,所以我不知道是什么毁了最终代码中的所有内容...这是完整的代码。抱歉,如果我犯了任何英语错误,那不是我的母语。
此代码...
for ( int x = -1; x <= 1; x++ ) { //Iterate through the 8 neighbour cells plus the one indicated
for ( int y = -1; y <= 1; y++ ) {
实际检查 9 单元格。也许您忘记了它检查(x,y(=(0,0(。那将包括细胞本身及其邻居。
一个简单的修复是:
for ( int x = -1; x <= 1; x++ ) { //Iterate through the 8 neighbour cells plus the one indicated
for ( int y = -1; y <= 1; y++ ) {
if (x || y) {
另外,simulate
函数(来自您的链接(犯了一个常见的错误,即在处理其旁边的单元格所需的状态更改之前,在同一数组中更新单元格的值。最简单的修复程序是保留两个数组 - 两个网格的两个完整副本(在您的代码中,两个ortho
数组(。从Orthoa阅读时,请更新Orthob。然后在下一代,翻转。从Orthob读取并写入Orthoa。
相关文章:
- 在细胞中产生噪声 - CPP 和 GLSL
- 优化在 Z^3 中查找邻居
- 将消息从节点发送到其邻居
- Nanoflann发现邻居提出段错误
- 最近的邻居用nanoflann搜索
- 在 3D 点云、GPU 中查找最近的邻居
- 检查数组的邻居值
- 在 std::set 中找到邻居
- 如何在任意网格上找到顶点的邻居
- 在cgal :: surface_mesh中找到某个原始的邻居
- 活着的邻居细胞未正确计数
- MPI 中的鬼细胞交换模式
- 多线程程序中的K最近的邻居
- 在Boost Graph库中选择给定顶点的随机进出邻居的有效方法
- 包括带有性细胞群的标题
- 生活的游戏邻居检查(更简单的方法可能吗?)
- 加快对图形中邻居的迭代
- 如何计算矩阵中的邻居数
- 生命游戏C++,检查邻居
- 如何找到4邻居拉普拉斯算子的卷积;这个节目有错吗