ellipse.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. function h=ellipse(ra,rb,ang,x0,y0,C,Nb)
  2. % Ellipse adds ellipses to the current plot
  3. %
  4. % ELLIPSE(ra,rb,ang,x0,y0) adds an ellipse with semimajor axis of ra,
  5. % a semiminor axis of radius rb, and an orientation of the semimajor
  6. % axis with an angle of ang (in radians) rotated counter-clockwise
  7. % from the x-axis. The ellipse is centered at the point x0,y0.
  8. %
  9. % The length of ra, rb, and ang should be the same.
  10. % If ra is a vector of length L and x0,y0 scalars, L ellipses
  11. % are added at point x0,y0.
  12. % If ra is a scalar and x0,y0 vectors of length M, M ellipse are with the same
  13. % radii are added at the points x0,y0.
  14. % If ra, x0, y0 are vectors of the same length L=M, M ellipses are added.
  15. % If ra is a vector of length L and x0, y0 are vectors of length
  16. % M~=L, L*M ellipses are added, at each point x0,y0, L ellipses of radius ra.
  17. %
  18. % ELLIPSE(ra,rb,ang,x0,y0,C)
  19. % adds ellipses of color C. C may be a string ('r','b',...) or the RGB value.
  20. % If no color is specified, it makes automatic use of the colors specified by
  21. % the axes ColorOrder property. For several ellipses C may be a vector.
  22. %
  23. % ELLIPSE(ra,rb,ang,x0,y0,C,Nb), Nb specifies the number of points
  24. % used to draw the ellipse. The default value is 300. Nb may be specified
  25. % for each ellipse individually, in which case it should be the same
  26. % length as ra, rb, etc.
  27. %
  28. % h=ELLIPSE(...) returns the handles to the ellipses.
  29. %
  30. % usage exmple: the following produces a red ellipse centered at 1,1
  31. % and tipped down at a 45 deg axis from the x axis
  32. % ellipse(1,2,pi/4,1,1,'r')
  33. %
  34. % note that if ra=rb, ELLIPSE plots a circle
  35. %
  36. % written by D.G. Long, Brigham Young University, based on the
  37. % CIRCLES.m original written by Peter Blattner, Institute of
  38. % Microtechnology, University of Neuchatel, Switzerland, blattner@imt.unine.ch
  39. % Check the number of input arguments
  40. if nargin<1,
  41. ra=[];
  42. end;
  43. if nargin<2,
  44. rb=[];
  45. end;
  46. if nargin<3,
  47. ang=[];
  48. end;
  49. if nargin<5,
  50. x0=[];
  51. y0=[];
  52. end;
  53. if nargin<6,
  54. C=[];
  55. end
  56. if nargin<7,
  57. Nb=[];
  58. end
  59. % set up the default values
  60. if isempty(ra),ra=1;end;
  61. if isempty(rb),rb=1;end;
  62. if isempty(ang),ang=0;end;
  63. if isempty(x0),x0=0;end;
  64. if isempty(y0),y0=0;end;
  65. if isempty(Nb),Nb=300;end;
  66. if isempty(C),C=get(gca,'colororder');end;
  67. % work on the variable sizes
  68. x0=x0(:);
  69. y0=y0(:);
  70. ra=ra(:);
  71. rb=rb(:);
  72. ang=ang(:);
  73. Nb=Nb(:);
  74. if isstr(C),C=C(:);end;
  75. if length(ra)~=length(rb),
  76. error('length(ra)~=length(rb)');
  77. end;
  78. if length(x0)~=length(y0),
  79. error('length(x0)~=length(y0)');
  80. end;
  81. % how many inscribed elllipses are plotted
  82. if length(ra)~=length(x0)
  83. maxk=length(ra)*length(x0);
  84. else
  85. maxk=length(ra);
  86. end;
  87. % drawing loop
  88. for k=1:maxk
  89. if length(x0)==1
  90. xpos=x0;
  91. ypos=y0;
  92. radm=ra(k);
  93. radn=rb(k);
  94. if length(ang)==1
  95. an=ang;
  96. else
  97. an=ang(k);
  98. end;
  99. elseif length(ra)==1
  100. xpos=x0(k);
  101. ypos=y0(k);
  102. radm=ra;
  103. radn=rb;
  104. an=ang;
  105. elseif length(x0)==length(ra)
  106. xpos=x0(k);
  107. ypos=y0(k);
  108. radm=ra(k);
  109. radn=rb(k);
  110. an=ang(k)
  111. else
  112. rada=ra(fix((k-1)/size(x0,1))+1);
  113. radb=rb(fix((k-1)/size(x0,1))+1);
  114. an=ang(fix((k-1)/size(x0,1))+1);
  115. xpos=x0(rem(k-1,size(x0,1))+1);
  116. ypos=y0(rem(k-1,size(y0,1))+1);
  117. end;
  118. % draw ellipse
  119. co=cos(an);
  120. si=sin(an);
  121. the=linspace(0,2*pi,Nb(rem(k-1,size(Nb,1))+1,:)+1);
  122. % x=radm*cos(the)*co-si*radn*sin(the)+xpos;
  123. % y=radm*cos(the)*si+co*radn*sin(the)+ypos;
  124. p=line(radm*cos(the)*co-si*radn*sin(the)+xpos,radm*cos(the)*si+co*radn*sin(the)+ypos);
  125. set(p,'color',C(rem(k-1,size(C,1))+1,:));
  126. % output handles to each ellipse if output variable specified
  127. if nargout > 0
  128. h(k)=p;
  129. end
  130. end;