DRAWLINE Draw a straight line clipped by the current axis. drawLine(LINE); Draws the line LINE on the current axis, by using current axis to clip the line. drawLine(LINE, PARAM, VALUE); Specifies drawing options. H = drawLine(...) Returns a handle to the created line object. If clipped line is not contained in the axis, the function returns -1. Example figure; hold on; axis equal; axis([0 100 0 100]); drawLine([30 40 10 20]); drawLine([30 40 20 -10], 'Color', 'm', 'LineWidth', 2); drawLine([-30 140 10 20]); See also: lines2d, createLine, drawEdge --------- author : David Legland INRA - TPV URPOI - BIA IMASTE created the 31/10/2003.
0001 function varargout = drawLine(lin, varargin) 0002 %DRAWLINE Draw a straight line clipped by the current axis. 0003 % 0004 % drawLine(LINE); 0005 % Draws the line LINE on the current axis, by using current axis to clip 0006 % the line. 0007 % 0008 % drawLine(LINE, PARAM, VALUE); 0009 % Specifies drawing options. 0010 % 0011 % H = drawLine(...) 0012 % Returns a handle to the created line object. If clipped line is not 0013 % contained in the axis, the function returns -1. 0014 % 0015 % Example 0016 % figure; hold on; axis equal; 0017 % axis([0 100 0 100]); 0018 % drawLine([30 40 10 20]); 0019 % drawLine([30 40 20 -10], 'Color', 'm', 'LineWidth', 2); 0020 % drawLine([-30 140 10 20]); 0021 % 0022 % See also: 0023 % lines2d, createLine, drawEdge 0024 % 0025 % --------- 0026 % author : David Legland 0027 % INRA - TPV URPOI - BIA IMASTE 0028 % created the 31/10/2003. 0029 % 0030 0031 % HISTORY 0032 % 25/05/2004 add support for multiple lines (loop) 0033 % 23/05/2005 add support for arguments 0034 % 03/08/2010 bug for lines outside box (thanks to Reto Zingg) 0035 % 04/08/2010 rewrite using clipLine 0036 % 2011-10-11 add management of axes handle 0037 0038 % extract handle of axis to draw in 0039 if isAxisHandle(lin) 0040 ax = lin; 0041 lin = varargin{1}; 0042 varargin(1) = []; 0043 else 0044 ax = gca; 0045 end 0046 0047 % default style for drawing lines 0048 if length(varargin) ~= 1 0049 varargin = [{'color', 'b'}, varargin]; 0050 end 0051 0052 % extract bounding box of the current axis 0053 xlim = get(ax, 'xlim'); 0054 ylim = get(ax, 'ylim'); 0055 0056 % clip lines with current axis box 0057 clip = clipLine(lin, [xlim ylim]); 0058 ok = isfinite(clip(:,1)); 0059 0060 % initialize result array to invalide handles 0061 h = -1 * ones(size(lin, 1), 1); 0062 0063 % draw valid lines 0064 h(ok) = plot(ax, clip(ok, [1 3])', clip(ok, [2 4])', varargin{:}); 0065 0066 % return line handle if needed 0067 if nargout > 0 0068 varargout = {h}; 0069 end