Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Matrix rotator

This method rotates a matrix
Example output:
~/Desktop% ruby rotate.rb
normal
12345
00000
fooba
rotated left
50a
40b
30o
20o
10f
rotated right
f01
o02
o03
b04
a05

def rotateMatrix(matrix, direction)
  # - You must Rotate the matrix neo!
  oldMap = matrix

  # Get the number of lines in the old map (they're the new columns)
  lineCount = oldMap.size
  # Get the number of columns in the old map (We have that many rows now)
  columnCount = oldMap[0].size
  @map = []
  columnCount.times { @map.push [] }

  # Loop through every line in the old map, retrieve the appropriate column
  # and make a horizontal column with it's contents
  # we'll take one (old)line at a time and rotate it.
  onLine = 0
  oldMap.each do |oldLine|
    onColumn = 0
    oldLine.each do
      case direction
      when :right
        @map[(columnCount - 1) - onColumn][(lineCount - 1) - onLine] = oldLine[(columnCount - 1) - onColumn]
      when :left
        @map[onColumn][onLine] = oldLine[(columnCount - 1) - onColumn]
      end
      onColumn += 1
    end
    onLine += 1
  end
  @map
end

def rotateRight(matrix)
  rotateMatrix(matrix, :right)
end

def rotateLeft(matrix)
  rotateMatrix(matrix, :left)
end

Java - rotate -90°/90°

public BufferedImage rotate90DX(BufferedImage bi)
	{
		int width = bi.getWidth();
		int height = bi.getHeight();
		
		BufferedImage biFlip = new BufferedImage(height, width, bi.getType());
		
		for(int i=0; i<width; i++)
			for(int j=0; j<height; j++)
				biFlip.setRGB(height-1-j, width-1-i, bi.getRGB(i, j));
		
		return biFlip;
	}


	public BufferedImage rotate90SX(BufferedImage bi)
	{
		int width = bi.getWidth();
		int height = bi.getHeight();
		
		BufferedImage biFlip = new BufferedImage(height, width, bi.getType());
		
		for(int i=0; i<width; i++)
			for(int j=0; j<height; j++)
				biFlip.setRGB(j, i, bi.getRGB(i, j));
		
		return biFlip;
	}

Array rotate //Javascript Function


Rotate the elements of an array with the minimum possible amount of movements.
It's thousands faster than using sequences of "array.unshift(array.pop())" or "array.push(array.shift())" due to memory moving on the "unshift" and "shift" methods. It also doesn't use a helper array, so it's fast and requires no aditional memory.

[UPDATED CODE CAN BE FOUND HERE]



//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/rotate [v1.0]

rotate = function(a /*array*/, p /* integer, positive integer rotate to the right, negative to the left... */){ //v1.0
    for(var l = a.length, p = (Math.abs(p) >= l && (p %= l), p < 0 && (p += l), p), i, x; p; p = (Math.ceil(l / p) - 1) * p - l + (l = p))
        for(i = l; i > p; x = a[--i], a[i] = a[i - p], a[i - p] = x);
    return a;
};


Usage

document.write(
    "rotate([1,2,3], 2) = ", rotate([1,2,3], 2), "<br />",
    "rotate([1,2,3], -2) = ", rotate([1,2,3], -2), "<br />",
    "rotate([1,2,3], 1000) = ", rotate([1,2,3], 1000), "<br />"
)

rotate function

    rotate: func [
        "Rotate values in a series."
        series [series!]
        /left   "Rotate left (the default)"
        /right  "Rotate right"
        /part range [number!] "Rotate this many positions"  ; TBD series! support?
        /local offset pad
    ][
        range: any [all [range  range // length? series] 1]
        if any [empty? series  zero? range] [return series]
        either right [
            offset: does [skip tail series negate range]
            pad: copy offset
            head insert head clear offset pad
        ][
            pad: copy/part series range
            append remove/part series range pad
        ]
    ]

ROR (rotate-right) function

    ROR: func ["Rotate Right" s [series!] n [integer!]][
        n: n // length? s
        append copy at s ((length? s) - (n - 1)) copy/part s (length? s) - n
    ]

ROL (rotate-left) function

    ROL: func ["Rotate Left" s [series!] n [integer!]][
        n: n // length? s
        append copy at s add n 1 copy/part s n
    ]
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS