tilting an ellipse on a given angle
46 次查看(过去 30 天)
显示 更早的评论
I am plotting ellipses as attached. My program is working fine but I need to tilt the ellipse on an angle (in degrees) entered by my user. The pivotal point is shown as black dot in attachment.
I am using following code to get each ellipse, few parameters e.g Q,R etc are received from another part of program.
xCenter = 2*Q;
yCenter = 2*R;
xRadius = Q;
yRadius = R;
theta = 0 : 0.01 : 2*pi;
x = xRadius * cos(theta) + xCenter;
y = yRadius * sin(theta) + yCenter;
e=plot(x, y, 'LineWidth', 1);
fill(x,y,'g')
axis square;
xlim([0 2*yCenter]);
ylim([0 2*yCenter]);
0 个评论
采纳的回答
Image Analyst
2016-11-21
You need to introduce a phase shift to get a rotation. See my demo code. It makes a rotated ellipse. Then it uses a second way, a rotation matrix, to rotate that ellipse by a specified angle.
% Creates a rotated ellipse, and then rotates it again by a specified angle using a second method: a rotation matrix.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
clearvars;
format longg;
format compact;
fontSize = 20;
% Parameterize the equation.
t = linspace(0, 360,1000);
phaseShift = 20;
xAmplitude = 2;
yAmplitude = 1;
x = xAmplitude * sind(t + phaseShift);
y = yAmplitude * cosd(t);
% Now plot the rotated ellipse.
plot(x, y, 'b-', 'LineWidth', 2);
axis equal
grid on;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
title('Rotated Ellipses', 'FontSize', fontSize);
text(-1.75, 1.4, 'Parametric --', 'Color', 'b', 'FontSize', fontSize);
% Now plot another ellipse and multiply it by a rotation matrix.
% https://en.wikipedia.org/wiki/Rotation_matrix
rotationAngle = 30;
transformMatrix = [cosd(rotationAngle), sind(rotationAngle);...
-sind(rotationAngle), cosd(rotationAngle)]
xAligned = xAmplitude * sind(t);
yAligned = yAmplitude * cosd(t);
xyAligned = [xAligned; yAligned]';
xyRotated = xyAligned * transformMatrix;
xRotated = xyRotated(:, 1);
yRotated = xyRotated(:, 2);
hold on;
plot(xRotated, yRotated, 'g-', 'LineWidth', 2);
% Plot a line at 30 degrees
slope = tand(30);
x1 = min(x(:));
y1 = slope * x1;
x2 = max(x(:));
y2 = slope * x2;
line([x1 x2], [y1 y2], 'Color', 'r');
text(-1.75, 1.25, 'Rotation Matrix --', 'Color', 'g', 'FontSize', fontSize);
text(-1.75, 1.1, '30 Degree Line --', 'Color', 'r', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
2 个评论
Image Analyst
2016-11-21
I think maybe you just didn't spend the time to go through the math, so I did it for you:
% Creates a rotated ellipse, and then rotate it again by specified angles using a second method: a rotation matrix.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
clearvars;
format long g;
format compact;
fontSize = 20;
darkGreen = [0, 0.6, 0];
% Parameterize the equation.
t = linspace(0, 360,1000);
xAmplitude = 1;
yAmplitude = 2.5;
xCenter = 2.5;
yCenter = 5;
xOriginal = xAmplitude * sind(t) + xCenter;
yOriginal = yAmplitude * cosd(t) + yCenter;
% Now plot the rotated ellipse.
plot(xOriginal, yOriginal, 'b-', 'LineWidth', 2);
axis equal
grid on;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
title('Rotated Ellipses', 'FontSize', fontSize);
xlim([-3, 8]);
ylim([-3, 8]);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized','OuterPosition',[0 0 1 1]);
drawnow;
hold on;
% Now plot more ellipses and multiply it by a rotation matrix.
% https://en.wikipedia.org/wiki/Rotation_matrix
% For each angle, subtract the center, multiply by the rotation matrix and add back in the center.
for rotationAngle = 10 : 20 : 350
transformMatrix = [cosd(rotationAngle), sind(rotationAngle);...
-sind(rotationAngle), cosd(rotationAngle)];
xAligned = (xOriginal - xCenter);
yAligned = (yOriginal - yCenter/2);
xyAligned = [xAligned; yAligned]';
xyRotated = xyAligned * transformMatrix;
xRotated = xyRotated(:, 1) + xCenter;
yRotated = xyRotated(:, 2) + yCenter/2;
hold on;
plot(xRotated, yRotated, '-', 'Color', darkGreen, 'LineWidth', 2);
end
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!