How can I rotate a set of points in a plane by a certain angle about an arbitrary point?
조회 수: 304 (최근 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!