function h = drawellipse(x,y,ang,sd1,sd2,theta0,theta1,linestyle,granularity) <x> is x-position of ellipse center <y> is y-position of ellipse center <ang> is the orientation in [0,2*pi). 0 means major axis is parallel to x-axis. <sd1> is the std dev along the major axis <sd2> is the std dev along the minor axis <theta0> (optional) is the starting angle in [0,2*pi). default: 0. <theta1> (optional) is the ending angle in [0,2*pi]. default: 2*pi. <linestyle> (optional) is like 'r-'. default: 'r-'. special case is {C} where C is a color char, scalar, or vector. in this case, a patch object is created instead of a line object. <granularity> (optional) is how many points in a complete revolution. default: 360. draw a complete or partial ellipse on the current figure. the ellipse corresponds to +/- 1 standard deviation along the major and minor axes. we proceed CCW from <theta0> to <theta1>. return the handle to the line object that we create. example: figure; drawellipse(3,1,pi/6,3,1,0,3*pi/2,'ro-',50); axis equal;
0001 function h = drawellipse(x,y,ang,sd1,sd2,theta0,theta1,linestyle,granularity) 0002 0003 % function h = drawellipse(x,y,ang,sd1,sd2,theta0,theta1,linestyle,granularity) 0004 % 0005 % <x> is x-position of ellipse center 0006 % <y> is y-position of ellipse center 0007 % <ang> is the orientation in [0,2*pi). 0 means major axis is parallel to x-axis. 0008 % <sd1> is the std dev along the major axis 0009 % <sd2> is the std dev along the minor axis 0010 % <theta0> (optional) is the starting angle in [0,2*pi). default: 0. 0011 % <theta1> (optional) is the ending angle in [0,2*pi]. default: 2*pi. 0012 % <linestyle> (optional) is like 'r-'. default: 'r-'. 0013 % special case is {C} where C is a color char, scalar, or vector. 0014 % in this case, a patch object is created instead of a line object. 0015 % <granularity> (optional) is how many points in a complete revolution. default: 360. 0016 % 0017 % draw a complete or partial ellipse on the current figure. the ellipse corresponds 0018 % to +/- 1 standard deviation along the major and minor axes. we proceed CCW from 0019 % <theta0> to <theta1>. return the handle to the line object that we create. 0020 % 0021 % example: 0022 % figure; drawellipse(3,1,pi/6,3,1,0,3*pi/2,'ro-',50); axis equal; 0023 0024 % input 0025 if ~exist('theta0','var') || isempty(theta0) 0026 theta0 = 0; 0027 end 0028 if ~exist('theta1','var') || isempty(theta1) 0029 theta1 = 2*pi; 0030 end 0031 if ~exist('linestyle','var') || isempty(linestyle) 0032 linestyle = 'r-'; 0033 end 0034 if ~exist('granularity','var') || isempty(granularity) 0035 granularity = 360; 0036 end 0037 0038 % deal with thetas for wrap-around case 0039 if theta1 < theta0 0040 theta1 = theta1 + 2*pi; 0041 end 0042 0043 % prep figure 0044 hold on; 0045 0046 % figure out thetas that we want 0047 thetas = linspace(theta0,theta1,ceil((theta1-theta0)/(2*pi) * (granularity+1))); 0048 0049 % figure out the base coordinates 0050 [X,Y] = pol2cart(thetas,1); 0051 coord = [X; Y]; 0052 0053 % scale 0054 coord = diag([sd1 sd2]) * coord; 0055 0056 % rotate [cos(ang) sin(ang); -sin(ang) cos(ang)] rotates CW 0057 coord = [cos(-ang) sin(-ang); -sin(-ang) cos(-ang)]*coord; 0058 0059 % translate [TODO: TRANSFORMATIONS SHOULD BE CLEANED UP AND MADE INTO FUNCTIONS!] 0060 coord(1,:) = coord(1,:) + x; 0061 coord(2,:) = coord(2,:) + y; 0062 0063 % do it 0064 if iscell(linestyle) 0065 h = patch(coord(1,:),coord(2,:),linestyle{1}); 0066 else 0067 h = plot(coord(1,:),coord(2,:),linestyle); 0068 end