How can I rotate a set of points in a plane by a certain angle about an arbitrary point?
100 views (last 30 days)
Show older comments
MathWorks Support Team
on 27 Jun 2009
Edited: MathWorks Support Team
on 30 Dec 2021
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.
Accepted Answer
MathWorks Support Team
on 22 Oct 2021
Edited: MathWorks Support Team
on 11 Jun 2021
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 Comments
Walter Roberson
on 16 May 2015
"%" 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
on 2 Sep 2015
Edited: MathWorks Support Team
on 30 Dec 2021
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?
More Answers (0)
See Also
Categories
Find more on 3-D Scene Control 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!