1

Traverse matrix in a direction specified by azimuth.

So I have a 2D matrix and an azimuth. What I want to do is traverse the matrix in that direction. From that azimuth I can derive a line equation.

Now I want to traverse the matrix in that direction of line. So in this case the 1st block I want to read info from is market as 1 then 2 and so on. (the values in the matrix right now are just to represent the order in which they are accessed)

https://i.stack.imgur.com/DPI9v.png

Let’s say if the line derived from azimuth goes as shown in the figure below then the matrix should be traverse from left to right.

https://i.stack.imgur.com/0D63q.png

If the problem is not clear please ask any question and I will explain further. Thanks for your time.

  • 3
    This looks a lot like a variant of Bresenham's line drawing algorithm http://en.wikipedia.org/wiki/Bresenham's_line_algorithm for which Google should overwhelm you with hits. – High Performance Mark Aug 17 '12 at 08:27

1 Answers1

0

Label cell in row i column j by i * sin(a) + j * cos(a). Then traverse in ascending order of these labels. Assuming in your first example a = 45° and in the second a = 0°.

So for your first example, sin(a) = cos(a) = sqrt(0.5). The calculated labels are the values in your figure multiplied by sqrt(2).

For your second example, sin(a) = 0; cos(a) = 1 and you get the same values as in your figure.

Henrik
  • 23,186
  • 6
  • 42
  • 92
  • @user1186700 - yes, this will even work for `a` outside the range [0°, 90°]. – Henrik Aug 17 '12 at 09:25
  • not sure how this helps...Perhaps I'm misinterpreting. Could you provide an example? – Rody Oldenhuis Aug 17 '12 at 09:30
  • @Henrik Thanks for your help. I think with a little bit of tweaking or more understanding I will implement this. I tried with the logic you gave. It was working fine from 0 degree to 45 degree. But later I found some issues. for a =90 I got this http://i.stack.imgur.com/B9IJ5.png And I was expecting this http://i.stack.imgur.com/IbJkJ.png – user1186700 Aug 23 '12 at 02:26
  • @user1186700 - sin(90°) = 1, cos(90°) = 0, i*sin(90°)+j*cos(90°) = i. So this should label all cells in the first row with 1, all cell in second row with 2... – Henrik Aug 23 '12 at 06:43
  • Remember to convert degrees to radians, so instead of 90° you pass pi/2 to sin() and cos(). – Henrik Aug 23 '12 at 08:54
  • @Henrik Thanks. I was using pi/2 for sin() and cos(). But I think what was happening was when sin or cos values used come -ve it used to have problems. So I just made sure that if the values are -ve I made them positive and it was all good. Thanks a lot for your help. I appreciate it. – user1186700 Aug 27 '12 at 01:10