function principal_rotation
%This program is to demonsrate the programming technique of drawing an ellipse
% with axes translation and rotation

% Plot the ellipsoid 
t=0:pi/20:2*pi;
%draw a circle
subplot(2,2,1),plot(sin(t),cos(t))
axis([-2 2 -2 2])
axis equal
title('A circle')
%畫正常的座標軸
draw_axes
%畫上data sets
x1=normrnd(0,0.6,100,1);
x2=normrnd(0,0.6,100,1);
hold on 
plot(x1,x2,'y*')
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%draw an ellipse
ellips_x=1*sin(t)';
ellips_y=0.5*cos(t)';
subplot(2,2,2),plot(ellips_x,ellips_y)
axis([-2 2 -2 2])
axis equal
title('An ellipse')
%畫正常的座標軸
draw_axes
%畫上data sets，偏向x軸的
x1=normrnd(0,0.6,100,1);
x2=0.3*normrnd(0,0.6,100,1);
hold on 
plot(x1,x2,'y*')
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Do rotation based on [1 1],[-1 1]
A=[1 -1;1 1];
C=(inv(A)*[ellips_x ellips_y]')';%Rotation(旋轉)
subplot(2,2,3),plot(C(:,1),C(:,2),'r--')
axis([-2 2 -2 2])
axis equal
title('A rotated ellipse')
%畫上data sets，旋轉過的
hold on
XX=(inv(A)*[x1';x2'])';
plot(XX(:,1),XX(:,2),'y*')
%畫旋轉的座標軸
z_x=-0.5:0.5;
z_y=z_x;
hold on
plot(z_x,z_y,'r--')
plot(z_x,-z_y,'r--')
%畫正常的座標軸
draw_axes

hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DO translation:center at (x_center,y_center)
x_center=1;
y_center=0.5;
D=[C(:,1)+x_center C(:,2)+y_center];%Translation(位移)
subplot(2,2,4),plot(D(:,1),D(:,2),'r:')
axis([-2 2 -2 2])
axis equal
title('A rotated and translated ellipse')
%畫上data sets，旋轉且移位過的
hold on
XX=(inv(A)*[x1';x2'])';
plot(XX(:,1)+x_center,XX(:,2)+y_center,'y*')
%畫旋轉且移位的座標軸
hold on
plot(z_x+x_center,z_y+y_center,'r--')
plot(z_x+x_center,-z_y+y_center,'r--')
draw_axes


hold off

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function draw_axes
x=-1.5:1.5;
hold on
plot(x,zeros(1,length(x)))
plot(zeros(1,length(x)),x)
hold off
