如何在负数空间中得到这样的相对坐标呢?
How can I get such relative coordinates even in negative number space?
想象一个简单的二维整数网格。它被一个更大的整数网格划分成块。给出了相等块的尺寸。
要从全局坐标中获得块,我可以简单地将坐标除以块大小并去掉小数点:chunk.x = global.x / chunksize.x
。这只适用于无符号数,因为负坐标不会四舍五入到正确的方向。因此,我手动向下四舍五入:chunk.x = (int)floor((float)global.x / chunksize.x)
。这工作得很好,但这里有另一部分。
我还想从全局坐标中计算相对于包含块的坐标。对于无符号数,我只使用余数:local.x = global.x % chunksize.x;
。但这对负坐标不起作用,因为负块的局部坐标不被镜像。
我怎么能计算局部坐标,即使在负数空间没有计算块之前?
This
const int M = 100000;
//chunk.x = (global.x + M*chunksize.x) / chunksize.x - M;
local.x = (global.x + M*chunksize.x) % chunksize.x;
应该比与浮点数的转换快得多。
或
//chunk.x = global.x / chunksize.x;
local.x = global.x % chunksize.x;
if (local.x < 0) {
//chunk.x--;
local.x += chunksize.x;
}
对于负面结果,将块大小添加到它们(这使它们为正)。如果你再取模,你会得到一个同样适用于正和负global.x
的表达式:
local.x = ((global.x % chunksize.x) + chunksize.x) % chunksize.x;
相关文章:
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 如何计算具有指定类型的表达式的相对精度和绝对精度
- 如何使用cppcheck处理半相对包含路径
- 为什么字符串比较的 == 运算符相对于任一字符串长度线性时间(似乎)?
- OpenGL 4.3 错误地将第 4 个纹理坐标映射到与第 3 个纹理坐标相同的位置
- 比较两个节点坐标的最佳方法是什么?
- 在 OpenGL 中将笛卡尔世界坐标转换为球面局部坐标
- 使用哈希图进行相对排序
- SDL2 调整窗口大小后如何缩放鼠标坐标?
- 如何在C++中获取坐标?
- 有没有办法保证析构函数的相对顺序?
- 如何将 x.y 坐标的值存储在不同的数组中
- Windows 和 Linux 之间的相对路径分辨率差异?
- 为什么从具有较大阵列的 SIMD 内部函数中获得的相对加速比标量更大?
- 如何在负数空间中得到这样的相对坐标呢?
- 测试汽车物体相对于世界坐标的碰撞
- Opengl将相对身体坐标转换为世界坐标
- 与包含拇指小部件的滑块小部件的相对坐标