NVIDIA Interview Question
Software Engineer / DevelopersFor the exact question, it might be better to use swap the content, however, maybe a rotation matrix is a more general solution ---- it is normally need transpose also.
Becasue the interview question is from the NVedia, I think rotate matrix might be what they want.
rotate: newx = x*cos(a) - y * sin(a)
newy = x * sin(a) + y * cos(a)
when a is 180degree, newx = -x newy = -y
we first move the origin to the center of the matrix, for an matrix with (m,n) dimension, the center is ((float)m/2, (float)n/2), where we suppose index start from 0; a more easier way is to multiply all the dimension by 2.
Following are the seudo code
for (x = 0; x < m; x++)
{
for (y = 0; y < n; y++)
{
// general case, but need handle the edging
/*
nx = ((m - 2*x)*cosa - (n - 2*y)*sina) >> 1;
ny = ((m - 2*x)*sina + (n - 2*y)*cosa) >> 1;
*/
/*We can simplify the question as m = n*/
N[nx][ny] = O[x][y];
}
}
- Vir Pratap Uttam May 04, 2015