二维数组的x,y和row,col属性是倒着的吗?

发布于 2024-08-20 11:45:41 字数 177 浏览 4 评论 0原文

如果我想到 x,y 坐标平面,x,y 是有序对的常见表示法,但如果我使用两角数组,我有 myArray[row][col] ,row 是 y,col 是x。这是倒退还是我只是想错了?我以为它看起来像 myArray[x][y] 但如果我想要真正的行和列(就像在游戏板中一样),那就错了。难道 myArray[y][x] 不会真正模仿行列板?

If I think of the x,y coordinate plane, x,y is the common notation for an ordered pair, but if I use a two-dime array I have myArray[row][col] and row is the y and col is the x. Is that backwards or am I just thinking about it wrong? I was thinking it would look like myArray[x][y] but that's wrong if I want real rows and columns (like in a gameboard.) Wouldn't it be myArray[y][x] to truly mimic a row column board?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

这个俗人 2024-08-27 11:45:41

你说得对,但确实感觉有点倒退。行号是 y 坐标,列号是 x 坐标,但我们通常写 row,col,但也通常写 x,y。

您是否要将数组写为 [y][x] 还是 [x][y] 主要取决于您对内存中数组布局的实际关心程度(如果您这样做,则取决于您使用的语言)。以及您是否想要编写可以单独操作行或列的函数/方法。

如果您正在编写 C/C++ 代码,数组将以 行主顺序 存储,这意味着单行数据可以视为一维数组。但单列数据却不能。如果我没记错的话,VB 使用列主顺序,因此语言有所不同。我很惊讶 C# 也不是行主序,但我不知道。

You have it right, and it does feel a bit backwards. The row number is a y coordinate, and the column number is an x coordinate, and yet we usually write row,col but we also usually write x,y.

Whether you want to write your array as [y][x] or [x][y] depends mostly on how much you actually care about the layout of your array in memory (and if you do, what language you use). And whether you want to write functions/methods that can operate on rows or columns in isolation.

If you are writing C/C++ code, arrays are stored in Row Major Order which means that a single row of data can be treated as 1 dimensional array. But a single column of data cannot. If I remember correctly, VB uses column major order, so languages vary. I'd be surprised of C# isn't also row major order, but I don't know.

甜味超标? 2024-08-27 11:45:41

这就是我为了自己的理智所做的:

int x = array[0].length;
int y = array.length;

然后对于我所做的每个数组调用,我写道:

array[y][x]

这对于图形算法和水平/垂直矩阵翻转特别有用。

This is what I do for my own sanity:

int x = array[0].length;
int y = array.length;

And then for every single array call I make, I write:

array[y][x]

This is particulary useful for graphing algorithms and horizontal/vertical matrix flipping.

不弃不离 2024-08-27 11:45:41

如何将数据存储在数组中([x][y] 或 [y][x])并不重要。重要的是你总是以连续的方式循环数组。 java 二维数组本质上是存储第二个数组的一维数组(例如,在 [y][x] 的情况下,您有一个 [y] 的长数组,其中每个 y 保存对应的 [x] 数组y 线)。

为了有效地运行整个数组,以某种方式访问​​数据非常重要,这样您就不必连续在该数组中进行搜索,从一个 y-array-of-xarrays 跳转到另一个 y-array-of- xarrays。您想要做的是访问一个 y 元素并访问其中的所有 x,然后再移动到下一个 y 元素。

所以在 Array[y][x] 的情况下。始终在外循环中使用第一个变量,在内循环中使用第二个变量:

for (int ys = 0; ys < Array.length; ys++)
    for (int xs = 0; xs < Array[y].length; xs++)
    {
        do  your stuff here
    }

当然,在循环之外预先分配两个 Array.length,以防止每个循环都必须获取这些值。

It doesn't matter how you store your data in the array ([x][y] or [y][x]). What does matter is that you always loop over the array in a contiguous way. A java two dimensional array is essentially a one dimensional array storing the second array (eg. in the case of [y][x] you have a long array of [y] in which each y holds the corresponding [x] arrays for that line of y).

To efficiently run through the whole array, it's important to access the data in a way so that you don't continuously have to do searches in that array, jumping from one y-array-of-xarrays to another y-array-of-xarrays. What you want to do is access one y element and access all the x's in there before moving to the next y element.

So in an Array[y][x] situation. always have the first variable in the outer loop and the second in the inner loop:

for (int ys = 0; ys < Array.length; ys++)
    for (int xs = 0; xs < Array[y].length; xs++)
    {
        do  your stuff here
    }

And of course pre-allocate both Array.lengths out of the loop to prevent having to get those values every cycle.

伏妖词 2024-08-27 11:45:41

我喜欢这个问题。你是绝对正确的。大多数时候我们要么思考(x,y),要么思考(行,列)。几年后我才提出质疑。然后有一天,我意识到我总是处理 for 循环,就好像 x 是一行,y 是一列一样,尽管在平面几何中它实际上是相反的。正如许多人提到的,在大多数情况下这并不重要,但一致性是一件很美好的事情。

I love the question. You’re absolutely right. Most of the time we are either thinking (x, y) or (row, col). It was years before I questioned it. Then one day I realized that I always processed for loops as if x was a row and y was a column, though in plane geometry it’s actually the opposite. As mentioned by many, it really doesn’t matter in most cases, but consistency is a beautiful thing.

说好的呢 2024-08-27 11:45:41

无论好坏,不一致的符号都是从数学中继承下来的。

多维数组遵循矩阵表示法,其中 Mi,j 表示行 i 和列 j 上的矩阵元素。

因此,如果用于表示矩阵,多维数组不会向后,但如果用于表示 2D 笛卡尔平面,其中 (x, y) 是坐标的典型排序。

另请注意,二维笛卡尔平面通常以 y 轴向上的方向定向。然而,这也与二维数组/矩阵的典型可视化方式(以及大多数光栅图像的坐标系)相背离。

For better or for worse, the inconsistent notation was inherited from math.

Multidimensional arrays follow matrix notation where Mi,j represents the matrix element on row i and column j.

Multidimensional arrays therefore are not backward if used to represent a matrix, but they will seem backward if used to represent a 2D Cartesian plane where (x, y) is the typical ordering for a coordinate.

Also note that 2D Cartesian planes typically are oriented with the y-axis growing upward. However, that also is backward from how 2D arrays/matrices are typically visualized (and with the coordinate systems for most raster images).

殤城〤 2024-08-27 11:45:41

事实上,这取决于你。你的问题没有思考的权利。例如,我通常将一维数组视为一行单元格。所以,在我看来它是 array[col][row]。但这真的取决于你...

Actually, It's up to you. There is no right of thinking in your question. For example i usually think of a one-dimension array as a row of cell. So, in my mind it is array[col][row]. But it is really up to you...

请爱~陌生人 2024-08-27 11:45:41

我敢打赌,对于这一点,会有很多不同的意见。最重要的是,只要你保持一致,这并不重要。如果您有其他库或类似的库将使用相同的数据,那么为了更轻松的集成而做的任何事情可能都是有意义的。

如果这严格在您自己的代码中,请执行您觉得舒服的任何操作。我个人偏好是使用 myArray[y][x]。如果它们很大,那么将您要同时多次访问的项目放在一起可能会带来性能优势。但如果有的话,直到很晚的阶段我才会担心这一点。

I bet there are a lot of differing opinions on this one. Bottom line is, it doesn't really matter as long as you are consistent. If you have other libraries or similar that is going to use the same data it might make sense to do whatever they do for easier integration.

If this is strictly in your own code, do whatever you feel comfortable with. My personal preference would be to use myArray[y][x]. If they are large, there might be performance benefits of keeping the items that you are going to access a lot at the same time together. But I wouldn't worry about that until at a very late stage if at all.

蓦然回首 2024-08-27 11:45:41

好吧,其实不然,如果你将一行视为 x 轴上的元素,然后二维数组是 y 轴上的一堆行元素,那么使用 y 对行进行操作是正常的,因为你已经知道 x (对于该特定行 x 始终相同,y 随其索引而变化),然后使用 x 对多个行元素进行操作(行垂直堆叠,每一行都有一个特定的 y 值)

Well not really, if you think of a row as elements on the x axis and then a 2d array is a bunch of row elements on the y axis, then it's normal to use y to operate on a row, as you already know the x (for that particular row x is always the same, it's y that's changing with its indices) and then use x to operate on the multiple row elements (the rows are stacked vertically, each one at a particular y value)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文