rotation about a point
12 次查看(过去 30 天)
显示 更早的评论
hello community, I'm slowly losing my mind trying to do something rather simple. I would like to rotate set of 2-d points about an arbibtrary point. I know that it amounts to an affine rotation and translation, but I cannot figure out the equation for the translation.
Below is my attempt on an example given by steve eddins. (http://blogs.mathworks.com/steve/2006/02/14/spatial-transformations-maketform-tformfwd-and-tforminv/)
it doesn't work. (note, the x and y vectors are too long to include here, here's the link for the data http://blogs.mathworks.com/images/steve/37/george.mat)
%point of rotation
xx = 0.2 yy = 0
%angle of rotation
t = pi/4;
%transform matrix !! this is where i'm messing up
A2 = [cos(t) -sin(t) 0; sin(t) cos(t) 0; xx-cos(t)*xx+sin(t)*yy yy-sin(t)*xx-cos(t)*yy 1];
%matlab tool for applying affine transform
tform2 = maketform('affine', A2); uv2 = tformfwd(tform2, [x y]);
%plot of results.
subplot(1,2,1) plot(x,y), axis ij, axis equal, axis([-2 2 -2 2]), grid on, title('George')
subplot(1,2,2) plot(uv2(:,1), uv2(:,2)), axis ij, axis equal, axis([-2 2 -2 2]) grid on title('Rotated by 45\circ')
thank you for your time! veritas
2 个评论
Jan
2011-3-1
It would be *very* helpful if you explain "it doesn't work" with any details. Please try to insert a specific question.
采纳的回答
更多回答(2 个)
Matt Fig
2011-3-1
Perhaps an example would work. You can use the function ROTATE to do what you want. I have shown an example, with minimal commenting so you should follow along carefully:
.
.
.
EDIT Fixed some coding errors.
S1 = subplot(1,2,1);
L = plot([1 2 3],[1 2 1],'b*');
title('Before Rotation')
xlim([-4 4])
axis equal
P = [0,0,1]; % Rotation vector
Rp = [.2 0]; % Point about which to rotate.
Xd = get(L,'xdata');
Yd = get(L,'ydata');
S2 = subplot(1,2,2);
L2 = plot(Xd,Yd,'*');
title('After Rotation')
hold on
plot(Rp(1),Rp(2),'*r')
XL = get(S1,'xlim');
YL = get(S1,'ylim');
Dx = diff(XL);
Dy = diff(YL);
for ii = 1:72
set(S2,'xlim',[Rp(1)-Dx Rp(1)+Dx],'ylim',[Rp(2)-Dy Rp(2)+Dy])
rotate(L2,P,5) % rotate 5 degrees at a time
xlim(XL)
ylim(YL)
pause(.05)
end
.
.
.
EDIT2
For a vectorized solution, you cold do something like this (with a nod to Paulo):
D = load('george'); % Load the data.
theta = pi/4; % Rotation angle.
plot(D.x,D.y);hold on
axis([-2 2 -2 2]);
P = [0 -.71]; % Rotation pivot point.
plot(P(1),P(2),'*k','MarkerSize',10); % Plot Pivot point.
r = [cos(theta) -sin(theta); sin(theta) cos(theta)];
T = bsxfun(@(x,y) r*x,[D.x.'-P(1);D.y.'-P(2)],false(1,length(D.x)));
plot(T(1,:)+P(1),T(2,:)+P(2),'r');
title('Original in Blue, Rotated in Red')
3 个评论
Matt Fig
2011-3-2
Yes, you basically have to first translate the points to the origin, then calculate the rotation matrix, then translate back.
gwoo
2022-10-25
Why couldn't this be:
T = (r * [D.x.' - P(1); D.y.' - P(2)])';
I'm not sure what the need for bsxfun is, unless 11 years ago one couldn't do that kind of expression without it.
Paulo Silva
2011-3-1
just for fun
theta=0;
load george
clf;hold on
plot(x,y);
axis([-2 2 -2 2]);
title('select rotation point');
[x0,y0]=ginput(1);
plot(x0,y0,'x','MarkerSize',10);
hp=plot(nan,nan,'r');
x=x-x0;y=y-y0;
for theta=0:pi/12:2*pi
r = [cos(theta) sin(theta); -sin(theta) cos(theta)];
xyr=[];for ii=1:numel(x),xyr=[xyr r*[x(ii);y(ii)]];,end
x1 = xyr(1,:)+x0;y1 = xyr(2,:)+y0;
set(hp,'XData',x1);set(hp,'YData',y1);
title(['Original (blue) Rotated by ' num2str(180*theta/pi) '\circ (red)'])
pause(0.1)
end
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!