mardi 1 novembre 2016

Rotating matrix by N degrees

I have a matrix A[M][M] and I want to rotate it N degrees relative to the center of the matrix, discarding the values which its new position is outside the original matrix and filling the missing values with zeroes. I am using the following formula to get he new positions:

newXPosition = ceil(cos(N)*(oldXPosition - M/2) - sin(N)*(oldYPosition - M/2) + M/2)
newYPosition = ceil(sin(N)*(oldXPosition - M/2) + cos(N)*(oldYPosition - M/2) + M/2)

However, this is failing at some point. If we look for newXPosition and newYPosition for oldXPosition = oldYPosition = 0, M = 32 and N = 90º, We get newXPosition = 32, newYPosition = 0. Taking into account that the dimensions are [0-31], it will not work to just substract one to newXPosition because in other occasions it would be newYPosition the variable that will have to be substracted, or even both.

Does anyone know where am I failing?

PS: I have already read a couple of answers regarding the 90 degrees rotation, but my intention is not to rotate the matrix 90 degrees, but N.

Aucun commentaire:

Enregistrer un commentaire