简体   繁体   English

如何在方形网格中查找具有单元格编号的坐标

[英]How to find coordinates with cell number in a square grid

I'm struggling to figure this out.. my brain is fogging up.我正在努力解决这个问题..我的大脑起雾了。

I have a 1000x1000 grid with the 0 in the bottom left (0,0) and top right would be (1000,1000)我有一个 1000x1000 网格,左下角为 0 (0,0),右上角为 (1000,1000)

I have 40000 squares (cells) that measure 5x5 and would like to calculate the coordinates given a number from 1-40000...我有 40000 个正方形(单元格),尺寸为 5x5,并且想计算给定数字 1-40000 的坐标...

Let's say square 13227 would be coordinates x and y (starting points)假设正方形 13227 将是坐标 x 和 y(起点)

Thanks:)谢谢:)

I would probably approach it in this way.我可能会以这种方式处理它。 I'd first start the calc based on the 5x5 grids, which really means that you've got 200x200 of those in your 1000x1000 grid.我首先会根据 5x5 网格开始计算,这实际上意味着您的 1000x1000 网格中有 200x200。

To find the row , get the floor - ie Math.floor() - of your number divided by 200.要找到该row ,请获取您的数字的地板 - 即Math.floor() - 除以 200。
In your example: Math.floor((13227/200)) = 66 .在您的示例中: Math.floor((13227/200)) = 66 So the corresponding 5x5 grid (for 13227) is going to be in row 66 (of the 5x5 grids)所以相应的 5x5 网格(对于 13227)将位于第 66 行(5x5 网格)

Then to get the column , subtract the (row number x 200) from the original number.然后得到column ,从原始数字中减去(行号 x 200)。
In your example: 13227 - (66 * 200) = 27 .在您的示例中: 13227 - (66 * 200) = 27 So it's going to be the 27th column (of the 5x5 grids)所以它将是第 27 列(5x5 网格)

Then I believe you should just be able to multiply those values by 5 to give you the bottom left-hand coordinate within your 1000x1000 grid然后我相信你应该能够将这些值乘以 5 来为你提供 1000x1000 网格内的左下角坐标

That is: x = 27 * 5 (135), y = 66 * 5 (330)即: x = 27 * 5 (135), y = 66 * 5 (330)

(Now that's just me roughing something out on paper and in my head, so you'll no doubt be able to tell me if I'm right or not, The only thing I'm not 100% sure on is whether my method is actually giving the bottom left-hand coordinate of the 5x5 square, but I'm pretty sure it is) (现在这只是我在纸上和脑海中粗略地做一些事情,所以你无疑可以告诉我我是否正确,我唯一不能 100% 确定的是我的方法是否正确实际上给出了 5x5 正方形的左下角坐标,但我很确定它是)

It is a classic problem in programming, converting 2D coordinates to 1D, it is usually done from 2D to 1D where indices start from 0 to length-1这是编程中的一个经典问题,将 2D 坐标转换为 1D,通常是从 2D 到 1D,其中索引从 0 开始到 length-1

First you have to figure out if you are in row-major or column-major:首先,您必须弄清楚您是行优先还是列优先: https://upload.wikimedia.org/wikipedia/commons/thumb/4/4d/Row_and_column_major_order.svg/170px-Row_and_column_major_order.svg.png

Most common case is row major.最常见的情况是行专业。 In this case imagine you have a 2D array with coordinates (x,y) in [0, width-1][0, height-1] and you want to convert to indices i in [0, width*height-1] .在这种情况下,假设您有一个二维数组,坐标(x,y)[0, width-1][0, height-1]中,并且您想转换为i[0, width*height-1]中的索引。

In row major, 2D->1D:行专业,2D->1D:

i = y * width + c

In row major, 1D->2D:在行专业中,1D->2D:

y = i / width  (euclidian division)
x = i % width  (modulo operation which compute the remainder of euclidian division)

Let's say you start numbering cells at (0,0) (botton left).假设您从 (0,0)(左下角)开始对单元格进行编号。 That's cell number 0 .那是单元格号码0
Next cell is the contiguous at right: c1(0,1).下一个单元格是右侧的连续单元:c1(0,1)。 And next is c2(0,2).接下来是 c2(0,2)。

When this row is full last cell is c1000(0,999), located at bottom right.当这一行已满时,最后一个单元格是 c1000(0,999),位于右下角。

So, next cell begins in a new row: c1000(1,0).因此,下一个单元格从新行开始:c1000(1,0)。 Next is c1001(1,1), etc.接下来是c1001(1,1)等。

Where is located cell c12345? c12345 单元格在哪里? Because you have a grid with rows length=1000 you must find how many rows fit in this number:因为您有一个行长度 = 1000 的网格,所以您必须找到适合此数字的行数:

numberOfRows= Math.floor(12345/1000)

which gives numberOfRows=12.这给出了 numberOfRows = 12。 There's a rest in the division, rest=345 .分区中有一个 rest , rest=345 So cell 12345 is in the row number 13 (the other 12 are already filled).所以单元格 12345 位于第 13 行(其他 12 行已填充)。 And is located at column=345并且位于column=345

Until now we've been calculating cells, now we want coordinates measured in distances.到目前为止,我们一直在计算单元格,现在我们想要以距离测量坐标。

Each cell is 5 units (inches or whatever).每个单元格为 5 个单位(英寸或其他)。 So cell 1 goes from 0 to 5, cell 2 from 5 to 10, and cell n goes from (n-1)*5 to n*5 .所以单元格 1 从 0 到 5,单元格 2 从 5 到 10,单元格n(n-1)*5 to n*5 same calculus for vertical distance.垂直距离的相同计算。

So cell 12345 corners, expressed in units, are (345-1)*5, 13*5 .因此,以单位表示的单元格 12345 角为(345-1)*5, 13*5 Notice I've used the "rest" (current column) for the left side and "13" (current row) for the upper side.请注意,我使用左侧的“rest”(当前列)和上部的“13”(当前行)。

The opposite problem is that you have coordinates (x,y) (in units, not in cells) and you want to know which cell corresponds to.相反的问题是你有坐标(x,y) (单位,而不是单元格)并且你想知道哪个单元格对应。
Dividing by the size of each cell you get row and column:除以每个单元格的大小,您得到行和列:

row = Math.floor(x/5)
col = Math.floor(y/5)

And knowing you have 1000 cells in a row you can get the cell number by:并且知道您连续有 1000 个单元格,您可以通过以下方式获取单元格编号:

cellNum = (row - 1)*1000 + col

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

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