Azzera filtri
Azzera filtri

rotation about a point

5 visualizzazioni (ultimi 30 giorni)
David
David il 1 Mar 2011
Commentato: gwoo il 25 Ott 2022
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 Commenti
Jan
Jan il 1 Mar 2011
It would be *very* helpful if you explain "it doesn't work" with any details. Please try to insert a specific question.
David
David il 2 Mar 2011
sorry, I guess I just meant that it was failing to do the point rotation, thus something is wrong with the matrix (specifically the translation as mentioned above)...and I'm not sure what the error is.

Accedi per commentare.

Risposta accettata

David
David il 2 Mar 2011
i found my error and here's the solution. thanks again for all your help!
clf xx = 0 yy = -0.7
t = pi/4; A = [cos(t) -sin(t) xx-cos(t)*xx+sin(t)*yy; sin(t) cos(t) yy-sin(t)*xx-cos(t)*yy; 0 0 1]
data = []; data = [x'; y'; ones(1,length(x))]
rotdat = A*data
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(rotdat(1,:), rotdat(2,:)), axis ij, axis equal, axis([-2 2 -2 2]) grid on title('Rotated by 45\circ')

Più risposte (2)

Matt Fig
Matt Fig il 1 Mar 2011
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 Commenti
Matt Fig
Matt Fig il 2 Mar 2011
Yes, you basically have to first translate the points to the origin, then calculate the rotation matrix, then translate back.
gwoo
gwoo il 25 Ott 2022
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.

Accedi per commentare.


Paulo Silva
Paulo Silva il 1 Mar 2011
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
  1 Commento
David
David il 2 Mar 2011
thx paulo! maybe you could see my above reply?

Accedi per commentare.

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by