MATLAB: Tilting an ellipse on a given angle

2d plotellipse

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]);

Best Answer

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]);