drawellipse.m 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. function h = drawellipse(x,y,ang,sd1,sd2,theta0,theta1,linestyle,granularity)
  2. % function h = drawellipse(x,y,ang,sd1,sd2,theta0,theta1,linestyle,granularity)
  3. %
  4. % <x> is x-position of ellipse center
  5. % <y> is y-position of ellipse center
  6. % <ang> is the orientation in [0,2*pi). 0 means major axis is parallel to x-axis.
  7. % <sd1> is the std dev along the major axis
  8. % <sd2> is the std dev along the minor axis
  9. % <theta0> (optional) is the starting angle in [0,2*pi). default: 0.
  10. % <theta1> (optional) is the ending angle in [0,2*pi]. default: 2*pi.
  11. % <linestyle> (optional) is like 'r-'. default: 'r-'.
  12. % special case is {C} where C is a color char, scalar, or vector.
  13. % in this case, a patch object is created instead of a line object.
  14. % <granularity> (optional) is how many points in a complete revolution. default: 360.
  15. %
  16. % draw a complete or partial ellipse on the current figure. the ellipse corresponds
  17. % to +/- 1 standard deviation along the major and minor axes. we proceed CCW from
  18. % <theta0> to <theta1>. return the handle to the line object that we create.
  19. %
  20. % example:
  21. % figure; drawellipse(3,1,pi/6,3,1,0,3*pi/2,'ro-',50); axis equal;
  22. % input
  23. if ~exist('theta0','var') || isempty(theta0)
  24. theta0 = 0;
  25. end
  26. if ~exist('theta1','var') || isempty(theta1)
  27. theta1 = 2*pi;
  28. end
  29. if ~exist('linestyle','var') || isempty(linestyle)
  30. linestyle = 'r-';
  31. end
  32. if ~exist('granularity','var') || isempty(granularity)
  33. granularity = 360;
  34. end
  35. % deal with thetas for wrap-around case
  36. if theta1 < theta0
  37. theta1 = theta1 + 2*pi;
  38. end
  39. % prep figure
  40. hold on;
  41. % figure out thetas that we want
  42. thetas = linspace(theta0,theta1,ceil((theta1-theta0)/(2*pi) * (granularity+1)));
  43. % figure out the base coordinates
  44. [X,Y] = pol2cart(thetas,1);
  45. coord = [X; Y];
  46. % scale
  47. coord = diag([sd1 sd2]) * coord;
  48. % rotate [cos(ang) sin(ang); -sin(ang) cos(ang)] rotates CW
  49. coord = [cos(-ang) sin(-ang); -sin(-ang) cos(-ang)]*coord;
  50. % translate [TODO: TRANSFORMATIONS SHOULD BE CLEANED UP AND MADE INTO FUNCTIONS!]
  51. coord(1,:) = coord(1,:) + x;
  52. coord(2,:) = coord(2,:) + y;
  53. % do it
  54. if iscell(linestyle)
  55. h = patch(coord(1,:),coord(2,:),linestyle{1});
  56. else
  57. h = plot(coord(1,:),coord(2,:),linestyle);
  58. end