drawrectangle.m 776 B

123456789101112131415161718192021222324252627282930
  1. function h = drawrectangle(x,y,szx,szy,linestyle)
  2. % function h = drawrectangle(x,y,sz1,sz2,linestyle)
  3. %
  4. % <x> is x-position of rectangle center
  5. % <y> is y-position of rectangle center
  6. % <szx> is size along the x-direction
  7. % <szy> is size along the y-direction. if [], default to <szx>.
  8. % <linestyle> (optional) is like 'r-'. default: 'r-'.
  9. %
  10. % draw a rectangle on the current figure.
  11. % return the handle to the line object that we create.
  12. %
  13. % example:
  14. % figure; drawrectangle(5,2,4,[],'r-'); axis equal;
  15. % input
  16. if ~exist('linestyle','var') || isempty(linestyle)
  17. linestyle = 'r-';
  18. end
  19. if isempty(szy)
  20. szy = szx;
  21. end
  22. % prep figure
  23. hold on;
  24. % do it
  25. h = plot([x-szx/2 x+szx/2 x+szx/2 x-szx/2 x-szx/2], ...
  26. [y+szy/2 y+szy/2 y-szy/2 y-szy/2 y+szy/2],linestyle);