Rotation of an ellipse
2 ビュー (過去 30 日間)
古いコメントを表示
Hello i am trying to rotate an ellipse, i was able to plot it , but now i want to put it in a matrix to then multiply it with a rotation Matrix. how do i do that ?
here is the code for plotting the ellipse
0 件のコメント
回答 (2 件)
Geoff Hayes
2016 年 2 月 6 日
Vlad - I couldn't quite get the ellipse to draw with the code that you have attached, so I modified it slightly to
function [x,y]=tracage_ellipse(xc,yc,a,b)
m = 1000;
x = zeros(m,1);
y = zeros(m,1);
theta = linspace(0,2*pi,m);
for k = 1:m
x(k) = a * cos(theta(k));
y(k) = b * sin(theta(k));
end
x = x + xc;
y = y + yc;
plot(x,y,'r--');
grid on;
Rather than plotting a single points on each iteration of the for loop, we plot the collection of points (that make up the ellipse) once we have iterated over the 1000 angles from zero to 2pi. This way we only draw one object (instead of a thousand) and x and y are now the arrays of all of these points (or coordinates) for the ellipse. Note also how we add transform or shift the ellipse whose origin is (0,0) to (xc,yc) after we have computed all of the coordinates.
To rotate, we can use the usual 2x2 rotation matrix
R = [cos(alpha) -sin(alpha); ...
sin(alpha) cos(alpha)];
where alpha is angle of the counter-clockwise rotation about the origin. If we wish to do a 45 degree (or pi/4) counter-clockwise rotation, then your above code becomes (or includes)
function [x,y]=tracage_ellipse(xc,yc,a,b)
m = 1000;
x = zeros(m,1);
y = zeros(m,1);
theta = linspace(0,2*pi,m);
for k = 1:m
x(k) = a * cos(theta(k));
y(k) = b * sin(theta(k));
end
alpha = pi/4;
R = [cos(alpha) -sin(alpha); ...
sin(alpha) cos(alpha)];
rCoords = R*[x' ; y']; % we use the [] to create a 2xm array
xr = rCoords(1,:)'; % extract the rotated x coordinates
yr = rCoords(2,:)'; % extract the rotated y coordinates
plot(x+xc,y+yc,'r--');
grid on;
hold on;
axis equal;
plot(xr+xc,yr+yc,'b--');
With the above, you should see the original ellipse drawn in red, and its rotated equivalent drawn in blue.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!