我有这个函数,我在python原型导航网格索引。它在python中工作得很好,但由于模运算符处理负数的方式,它在c++中就失效了。谁能建议一个修改,使它工作?
当坐标nx
和ny
为负时,函数失败。
int wrap_grid(int index, int x, int y, int nx, int ny) {
//first calculate positon on row by adding nx -
//assuming an infinite grid of indices (no limits or wrap)
int a = (index + (y * nx));
//then wrap around the width (x) of the row
int b = a % (x * y);
//now do column, calculate the bottom index of the column
int start = b - b % y;
//and the top index
int limit = b - b % y + y;
//now wrap the ny value around the columns
return start + (b + ny) % (limit - start);
}
EDIT: To explain function arguments
index
是矩形网格的索引,如下所示:
你只需要一个mod函数,只会给你非负值。直接的方法如下所示(为了清晰起见,先移动一下代码)。
#include <tuple>
#include <iostream>
int grid_coords_to_index(int cols, int rows, int x, int y) {
// wrap the coordinates...
int column = x % cols;
column = column >= 0 ? column : column + cols;
int row = y % rows;
row = row >= 0 ? row : row + rows;
int bottom_of_column_index = column * rows;
return bottom_of_column_index + row;
}
std::tuple<int,int> index_to_grid_coords(int cols, int rows, int index) {
// TODO: handle negatives correctly here too, if we cannot
// assume indices are positive.
return { index / rows, index % rows };
}
int wrap_grid(int initial_index, int cols, int rows, int x_offset, int y_offset) {
auto [x, y] = index_to_grid_coords(cols, rows, initial_index);
return grid_coords_to_index(cols, rows, x + x_offset, y + y_offset);
}
int main()
{
/*
5 11 17
4 10 16
3 9 15
2 8 14
1 7 13
0 6 12
index 9 is (1,3) so two to the left and one down should be 14 given wrapping
*/
std::cout << wrap_grid(9, 3, 6, -2, -1) << "n";
}
相关内容
- 计算二维网格点之间最短的距离,Python
- 我如何确保我的三维网格只跟随我的鼠标在x上?
- 如何使用godot三维网格
- 两个变量在三维网格上的插值
- 包裹二维网格索引
- 在平面上投影三维三角形网格时,如何得到仅可见表面的二维网格
- 如何在二维网格上形成一个盒子形状,无论点数多少
- for循环中二维网格迷宫的显示周期
- 使用Python从二维网格图的索引中恢复节点
- Python中二维网格图中的邻接矩阵问题
- SciPy:在三维网格上插入分散的数据
- 试图了解如何使用变量交换来更新二维网格
- 使用Collider Unity将线渲染转换为三维网格
- 三维网格上的点的索引是多少
- 二维网格如何在给定节点数的情况下找到索引
- 从二维网格中的值获取索引
- 从二维网格 javascript 中查找一维数组上的数组索引
- CUDA:三维网格中的全局唯一线程索引
- 在numpy中索引三维网格数据的球面子集
- 有效地求出非矩形二维网格上最近点的索引
最新更新
- 在Swift中从实时数据库Firebase中获取数据
- Devstack单一接口不能在ubuntu 20.04上工作
- Typescript没有安装
- 函数式语言类型推断混乱
- 使用Python从大文件解析数字数据时提高速度
- 如何从Multipass共享文件夹到主机?
- 从CMD或批处理文件中禁用"Notify me when the clock changes"设置
- 在Dockerfile中设置——net=host ?
- 如何在React中将arrayBuffer转换为JSON
- 是否有可能将html响应转换为json在扑动?
- 快速过滤numpy数组值的方法
- C保存字符串的数组列表
- 需要minio film配置建议
- 如何应用CSS字体大小"relative to what it would be originally"?
- 该应用程序在个人帐户中未绑定脚本时被阻止错误
- 将原始查询转换为django orm
- 如何从出现次数和值的列表中创建一个新列表
- 使用Julia中的Julia Broadcasting根据数组的索引计算数组值
- 在javascript中使用条件更新嵌套数组
- for循环多个条件
- 将第一列中的名称行转换为r中的列
- Nx张量的映射切片
- Dotnet Core Azure功能(隔离进程)如何加载应用程序.每个环境的Json
- Discordjs不发送消息
- 边框在css中不显示
- Python pandas中的深度嵌套JSON规范化
- 如何在JSON模式中从正确的对象中选择特定的字段
- 我可以在REST API中直接通过POST发送电话号码吗?
- "onPressed: () {}"在颤振中不起作用
- FbLitho在RecyclerCollectionComponent中重复视图,即使加载了新部分
热门标签:
javascript python java c# php android html jquery c++ css ios sql mysql arrays asp.net json python-3.x ruby-on-rails .net sql-server django objective-c excel regex ruby linux ajax iphone xml vba spring asp.net-mvc database wordpress string postgresql wpf windows xcode bash git oracle list vb.net multithreading eclipse algorithm macos powershell visual-studio image forms numpy scala function api selenium