answersLogoWhite

0

A picture is just a two-dimensional matrix of pixel colour values. However, the element type is of no importance to the algorithm so, for the purpose of this answer, we'll use a matrix of characters to better illustrate the steps required.

Given the following matrix:

A B C

X Y Z

The rotated matrix would be:

X A

Y B

Z C

Thus given a matrix M[X][Y], the rotated matrix N will be N[Y][X]. In other words, the dimensions are transposed. So before we can rotate we first need to transpose the matrix:

for a = 0 to X

for b = 0 to Y

N[b][a] = M[a][b]

next b

next a

At this point, N has the following layout:

A X

B Y

C Z

To complete the rotation, we simply reverse each row:

for a = 0 to Y

for b = 0 to X / 2

swap (N[a][b], N[a][X-b])

next b

next a

To rotate 270 degrees clockwise (or 90 degrees anti-clockwise), we reverse the columns after transposing:

for b = 0 to X

for a = 0 to Y / 2

swap (N[a][b], N[Y-a][b])

next a

next b

To rotate by 180 degrees, we reverse each row and then reverse each column (without transposing).

Note that all these rotations have a time complexity of O(X*Y*2) because every element has to be written twice. Space complexity is O(X*Y) for transposes and O(1) for reversals. However, when X is equal to Y (square matrix), transposing can be achieved in-place by reversing elements across the primary diagonal.

User Avatar

Wiki User

8y ago

What else can I help you with?

Related Questions

Which way do you rotate in volleyball clockwise or counterclockwise?

You rotate in a clockwise fashion


What is the two planet does not rotate in contour clockwise?

planet does not rotate in contour clockwise


What is the rotate feature?

The rotate feature allows you to change the orientation of an object or image by rotating it clockwise or counterclockwise. This can be useful for adjusting the alignment or layout of elements in a design or document.


Do hurricanes rotate clockwise or counterclockwise?

clockwise


Do typhoons rotate clockwise?

In the northern hemisphere, typhoons rotate counter-clockwise. In the southern they rotate clockwise. This is due to the force of the rotation of the Earth.


How do you rotate Photoshop brushes?

open a blank document then grab a brush palette.Pick a pattern anything but round in shape from brushes palette.In the brush palette select brush tip shape.Check out for options and flip x, flip y or any angle.


Do planets rotate clockwise or counterclockwise?

They spin clockwise


Does a team rotate clockwise to serve?

Yes. A team does rotate clockwise to serve. After a side out in favor of your team, each person in the back row will rotate one zone to their left, and each person in the front row will rotate one zone to their right (clockwise).


Do All Chrysler s disributers rotate counter clockwise?

No, the 318 and 360 , among others, rotate clockwise.


Which way does a typhoon rotate?

In the Northern Hemisphere, typhoons rotate counterclockwise. In the Southern Hemisphere, they rotate clockwise. This rotation is due to the Coriolis effect caused by the Earth's rotation.


Which planets do not rotate counter-clockwise?

Venus and Uranus are the only planets that do not rotate counter-clockwise.


What does it mean to rotate clockwise?

To rotate clockwise means to move in a circular motion to correspond with the way hands of a clock move