/src/utils/ellipse.m
http://slac-lucretia.googlecode.com/ · MATLAB · 55 lines · 23 code · 5 blank · 27 comment · 2 complexity · f4bd540e7a278f82e0beb39ead665c9e MD5 · raw file
- function [x,y]=ellipse(X,p,q)
-
- % function [x,y]=ellipse(X,p,q)
- %
- % Return coordinates which can be used to plot the ellipse described by the
- % 2x2 symmetric matrix "X", centered at the point (p,q).
- %
- % INPUTS:
- %
- % X: a 2x2 symmetric matrix which describes the ellipse as follows:
- %
- % [x y]*X*[x y]' = 1,
- %
- % or, with X = [a b
- % b c],
- %
- % ax^2 + 2bxy + cy^2 = 1;
- %
- % for a beam ellipse, X=inv(sigma)=(1/emit)*[gamma alpha
- % alpha beta]
- %
- % p: ordinate of ellipse center
- % q: abscissa of ellipse center
- %
- % OUTPUTS:
- %
- % x: a 1001 element column vector of x-coordinates
- % y: a 1001 element column vector of y-coordinates
-
- %===============================================================================
-
- [r,c]=size(X);
- if ((r~=2)|(c~=2))
- error('X must be a 2x2 matrix')
- end
- if (abs(X(1,2)-X(2,1))>(abs(.001*X(1,2))+eps))
- error('X must be a symmetric matrix')
- end
-
- a=X(1,1);
- b=X(1,2);
- c=X(2,2);
-
- delta=0.001;
- theta=[0:delta:pi]';
- C=cos(theta);
- S=sin(theta);
-
- r=sqrt((a*(C.^2)+2*b*(C.*S)+c*(S.^2)).^(-1));
- r=[r,r];
- C=[C,-C];
- S=[S,-S];
-
- x=r.*C;
- y=r.*S;