Rotating a meshgrid by 30 degrees?
5 views (last 30 days)
Show older comments
Is there a better way to do this in matlab other than multiplying it by a rotation matrix? I was also not sure on how to do so since the only rotation matrices I know are of sizes 2 x 2 or 3 x 3. I made a meshgrid of input 64 and the output was a very large vector array.
clear all;close all;clc;
%% (a) resize images with or without anti-aliasing filter
A = imread('filename');
B = imresize(A, [256, 256]);
N = 64;
[xx,yy] = meshgrid(1:N);
0 Comments
Answers (1)
Walter Roberson
on 27 Jan 2021
For that particular angle:
syms XX YY
r32 = sqrt(3)/2;
rx = XX*r32 + YY/2;
ry = -XX/2 + YY*r32;
However, this assumes rotation around (0,0), which if you were using image() or imshow() and defaults, would correspond to the top left corner. If you wanted to rotate around the middle, by far the easiest way to calculate it would be with a rotation matrix:
N = 64;
syms XX YY
M = makehgtform('translate',[-N/2 -N/2 0], 'zrotate', pi/6, 'translate', [N/2 N/2 0]);
rot_formula = (M * [XX;YY;0;1]).';
[xx, yy] = ndgrid(1:N);
rx_formula(XX,YY) = rot_formula(1);
ry_formula(XX,YY) = rot_formula(2);
rx = double(rx_formula(xx,yy));
ry = double(ry_formula(xx,yy));
scatter(xx(:), yy(:), 'k');
hold on
axis equal
scatter(rx(:), ry(:), 'b');
hold off
3 Comments
Walter Roberson
on 27 Jan 2021
Habit. ndgrid is more consistent than meshgrid as you go into higher dimensions. The first output for ndgrid have values from the first input to ndgrid, but for meshgrid, the first output for meshgrid has values from the second input to meshgrid.
There are a few older functions that need meshgrid style meshes, such as surf(), but a lot of the time it does not matter whether you use meshgrid or ndgrid for 2D or 3D data. For more then 3D then you must use ndgrid() as meshgrid does not generalize to 4 or more dimensions.
See Also
Categories
Find more on Geometric Transformation and Image Registration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!