math - Rotate a matrix(2D Array) by 45 degrees in c++ -
i'm trying rotate matrix(mxn)by 45 degrees. example:
|a|b|c|d|e| |f|g|h|i|j| |k|l|m|n|o|
would rotated this:
| | |a| | | | | | |f| |b| | | | |k| |g| |c| | | | |l| |h| |d| | | | |m| |i| |e| | | | |n| |j| | | | | | |o| | |
any appreciated! edit: on problem.
i have been thinking , formula ended with: f(i,j) = (i+j,m-1-i+j)
but problem run if rotation 2 times wouldn't 90 degree rotation.
if wanted rotate point (x,y) 45° use formula
x' = cos(45°) * x - sin(45°) * y y' = sin(45°) * x + cos(45°) * y
now know cos(45°) = sin(45°) = 1 / sqrt(2). if (x,y) integer coordinated (x',y') not integers due sqrt(2) factor.
if want rotate elements in matrix points lie in matrix want rotate integer coordinates integer coordinates (taking central point h origin). in sense, asking impossible.
there 2 ways around this.
1) drop requirement integer coordinates. not possible if want fit results in array. if array represents pixels in image do. once rotated need calculate resulting pixel values averaging surrounding pixels.
2) multiply results scale factor of sqrt(2). have done. if calculate distance in rotated grid using pythagoras distances of adjacent points sqrt(2).
if repeat twice find distances doubled.
one possible solution use algorithm , divide coordinates 2.
Comments
Post a Comment