Rotating a section of a two dimensional array
I've read this SO question on how to rotate a two-dimensional array many times, and I was curious as to how you could expand this situation to work with a section of a two-dimensional array. I've thought about it for a while, and I can't seem to come up with a good equation. Essentially what I'm wanting to do is something like this:
1 2 3 4 5 6 13 7 1 4 5 6 7 8 9 10 11 12 14 8 2 10 11 12 13 14 15 16 17 18 15 9 3 16 17 18 19 20 21 22 23 24 ----> 19 20 21 22 23 24 25 26 27 28 29 30 25 26 27 28 29 30 31 32 33 34 35 36 31 32 33 34 35 36
I'm writing this in Ruby, currently, but I don't particularly care what l开发者_开发知识库anguage the solution to this kind of problem would be. I'm just curious as to how you'd go about solving this kind of problem.
Edit: To add some more specifics, the basic parameters for a function that could do this would be something that looked like this
def rotate(array, times=1, x=0, y=0, len=nil)
...
end
Check this question : How do you rotate a two dimensional array?
there is a simple way :
a 2-dim array, use
transpose.map &:reverse
Something like that?
def rotate a,len,ii=0,jj=0,t=1
t.times do
a = a.map.with_index do |line,i|
line.map.with_index do |e,j|
(ii...(ii+len))===i && (jj...(jj+len))===j ?
a[ii+jj+len-j-1][jj+i-ii] : e
end
end
end
a
end
t = (1..6).map{|i|(1..6).map{|j|j+6*i-6}}
t.each { |i| p i }
rotate(t,3,2,1).each { |i| p i }
rotate(t,6).each { |i| p i }
rotate(t,3,2,1,4).each { |i| p i }
rotate(t,6,0,0,4).each { |i| p i }
rotate(t,2,2,2,3).each { |i| p i }
精彩评论