answersLogoWhite

0


Best Answer

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

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you rotate a image clockwise?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Which way do you rotate in volleyball clockwise or counterclockwise?

You rotate in a clockwise fashion


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.


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

planet does not rotate in contour clockwise


Do hurricanes rotate clockwise or counterclockwise?

clockwise


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.


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 planets rotate clockwise or counterclockwise?

They spin clockwise


Which way does the moon rotate on its axis clockwise or counter-clockwise?

counter clockwise


Do All Chrysler s disributers rotate counter clockwise?

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


Which way does a typhoon rotate?

counter clockwise counter clockwise


Do tornadoes rotate clockwise or counterclockwise?

In all but the rarest cases, tornadoes in the northern hemisphere rotate counterclockwise, while ones in the southern hemisphere rotate clockwise.


Which planets do not rotate counter-clockwise?

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