如何知道具有相同大小的方形单元格的旋转网格中的点的位置

How to know the location of a point in a rotated grid with equal sized square-shaped cells

给定一个以 θ (theta) 角度倾斜的网格,该网格具有大小相等的方形单元格,索引为 00、01、02、...、55(其中第一个数字是 x 索引,第二个数字是y 索引,例如00表示第0行第0列交叉处的单元格)和一个点p(x,y),我们如何在不检查所有单元格的情况下知道该点位于哪个网格单元格中?

例如,在下图中,点 p 位于索引为 23 的单元格中。

我在 Checking if a point is inside a rotated rectangle 找到了一个答案,它解释了如何确定一个点是否位于旋转矩形内,但是使用这种方法我需要检查所有网格单元。

也许是最简单的方法 - 将点旋转相同的角度(带负号)并检查 axis-aligned 网格中的新坐标

nx = x * cos(theta) - y * sin(theta)
ny = x * sin(theta) + y * cos(theta)
row = (int) (ny / cellsize)
col = (int) (nx / cellsize)