How can I rotate a set of points in a plane by a certain angle about an arbitrary point?
118 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2009-6-27
编辑: MathWorks Support Team
2021-12-30
I have a set of data points, and I would like to rotate them counterclockwise in the plane by a certain angle about a specific point in the same plane.
采纳的回答
MathWorks Support Team
2021-10-22
编辑:MathWorks Support Team
2021-6-11
You can rotate your data samples by multiplying the matrix of samples by a rotation matrix. A rotation matrix which creates a counterclockwise rotation of angle 'theta' about the origin in the 2-D plane can be created as follows:
R = [cos(theta) -sin(theta); sin(theta) cos(theta)];
For more information on rotation matrices you may refer to: https://en.wikipedia.org/wiki/Rotation_matrix
If, instead of rotating about the origin you wish to rotate about a specific point in the plane, you can first shift the points in the plane so that the desired center of rotation moves to the origin. Then you can do the rotation about the origin with the above matrix. Finally, after the rotation is done, shift the points back so that the center of origin is restored.
Here is an example of how this could be done in MATLAB:
% define the x- and y-data for the original line we would like to rotate
x = 1:10;
y = 1:10;
% create a matrix of these points, which will be useful in future calculations
v = [x;y];
% choose a point which will be the center of rotation
x_center = x(3);
y_center = y(3);
% create a matrix which will be used later in calculations
center = repmat([x_center; y_center], 1, length(x));
% define a 60 degree counter-clockwise rotation matrix
theta = pi/3; % pi/3 radians = 60 degrees
R = [cos(theta) -sin(theta); sin(theta) cos(theta)];
% do the rotation...
s = v - center; % shift points in the plane so that the center of rotation is at the origin
so = R*s; % apply the rotation about the origin
vo = so + center; % shift again so the origin goes back to the desired center of rotation
% this can be done in one line as:
% vo = R*(v - center) + center
% pick out the vectors of rotated x- and y-data
x_rotated = vo(1,:);
y_rotated = vo(2,:);
% make a plot
plot(x, y, 'k-', x_rotated, y_rotated, 'r-', x_center, y_center, 'bo');
axis equal
2 个评论
Walter Roberson
2015-5-16
"%" marks the start of a comment. So "% do the rotation..." is a comment remarking that the code that follows puts the rotation into effect.
Notice that this code applies to a set of point coordinates, a list of x and y, not to an image.
Mike Garrity
2015-9-2
编辑:MathWorks Support Team
2021-12-30
Its exactly the same except that the matrix is a 3x3 instead of a 2x2. So the example of rotating by theta around the Z axis becomes:
R = [cos(theta) -sin(theta) 0; ...
sin(theta) cos(theta) 0; ...
0 0 1];
If you want to rotate around a different axis in 3D, you might want to check out the makehgtform function. It has a simpler syntax that lets you do that case like this:
R = makehgtform('zrotate',theta)
As well as options for rotating around the other two axes, or around an arbitrary direction vector.
Except... The makehgtform function returns a 4x4. This allows it to represent translations. For rotations, you'll just want to use the first 3 rows and columns:
R = makehgtform('zrotate',theta);
R = R(1:3,1:3);
s = randn(3,500);
center = repmat([1 1 1]',1,500);
so = R*(s-center) + center;
Does that make sense?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!