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.
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.
Thanks guys.
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)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 callf(x, y) = scalarProduct((x, y), v)Sort the values of
f(x, y)and you’ve got the “traversing” you’re looking for!A real example.
Your matrix is 3×3
The scalar products are :
(0,0).(1,1) = 0
(0,1).(1,1) = 1
(0,2).(1,1) = 2
(1,0).(1,1) = 1
(1,1).(1,1) = 2
(1,2).(1,1) = 3
(2,0).(1,1) = 2
(2,1).(1,1) = 3
(2,2).(1,1) = 4
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)…
And if you want to do it with the angle 20, replace all occurences of
v=(1, 1)withv=(cos(20), sin(20))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.