在一个角度遍历一个二维数组 [英] Traversing a 2D array in an angle
问题描述
一般我们遍历数组行或列,但在这里我要遍历它的角度。 我会尽量解释一下我的意思是, 因此,可以说,如果角度为45度,然后,而非行由山坳它会搜索为(0,0),则(0,1)(1,0)则(0,2),(1,1),(2 ,0)等等......。(抱歉,不能上传图片,因为我是新用户,并不允许这样做,可能会尝试和想象/画一个阵列,这将有助于得到什么,我想说的) 但是,如果用户输入像20度的角度,我们如何才能确定如何搜索阵列会发生什么。
Generally we traverse the array by row or column but here I want to traverse it in an angle. I will try and explain what I mean, So lets say if the angle is 45 degree then rather than row by col it would search as (0,0) then (0,1) (1,0) then (0,2) , (1,1) ,(2,0) and so on.. .(sorry could not upload an image as I am new user and not allowed to do so, may be try and imagine/draw an array that would help get what I am trying to say) But what will happen if the user inputs an angle like 20 degree how can we determine how to search the array.
我只是想知道是否有任何算法,做类似的东西?编程语言不是问题,我想这个问题是比较algoritham几分。 任何想法将受到欢迎。 请随时问,如果我不能解释清楚我所期待的。
i just wanted to know if there is any algorithm which does something similar to this? Programming language is not an issue i guess the issue is more of algoritham sort. Any ideas would be welcome. Please feel free to ask if I am not able to explain clearly what I am looking for.
谢谢你们。
推荐答案
容易。以一个角度(比方说45)。这相当于你的情况一个vector V =(1,1)
。 (这可以被归一化到一个单一矢量(SQRT(2)/ 2,的sqrt(2)/ 2)
,但是这不是必须的)
Easy. Take an angle (let's say 45). This corresponds to a vector v=(1, 1)
in your case. (This can be normalized to a unitary vector (sqrt(2)/2, sqrt(2)/2)
, but this is not necessary)
有关您的阵列中每一个点,你有自己的坐标(X,Y)
。只要做到这些坐标与向量的数量积。让我们把 F(X,Y)= scalarProduct((X,Y),V)
For every single point in your array, you have their coordinates (x, y)
. Simply do the scalar product of these coordinates with the vector. Let's call f(x, y) = scalarProduct((x, y), v)
之类的 F(X,Y)
的值,你已经得到了穿越你要找的!
Sort the values of f(x, y)
and you've got the "traversing" you're looking for!
一个真实的例子。 你的矩阵是3×3 标量的产品有:
A real example. Your matrix is 3x3 The scalar products are :
(0,0)。(1,1)= 0
(0,0).(1,1) = 0
(0,1)。(1,1)= 1
(0,1).(1,1) = 1
(0,2)。(1,1)= 2
(0,2).(1,1) = 2
(1,0)。(1,1)= 1
(1,0).(1,1) = 1
(1,1)。(1,1)= 2
(1,1).(1,1) = 2
(1,2)。(1,1)= 3
(1,2).(1,1) = 3
(2,0)。(1,1)= 2
(2,0).(1,1) = 2
(2,1)。(1,1)= 3
(2,1).(1,1) = 3
(2,2)。(1,1)= 4
(2,2).(1,1) = 4
如果您通过升序订购这些标产品,您获得的顺序(0,0),(1,0),(1,0),(2,0),(1,1),(0, 2),(2,1)...
If you order these scalar products by ascending order, you obtain the ordering (0,0), (1,0), (1,0), (2,0), (1,1), (0,2), (2,1)...
如果你想用角20这样做,将 V =(1,1)
中所有出现的与 V =(COS (20),罪(20))
And if you want to do it with the angle 20, replace all occurences of v=(1, 1)
with v=(cos(20), sin(20))
下面是一个几何间pretation的说明。标量的产品对应于矢量v与蓝线的交点(红色)。
Here's an illustration of a geometrical interpretation. The scalar products correspond to the intersections of the vector v (in red) with the blue lines.
这篇关于在一个角度遍历一个二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!