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, clipLine
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, clipLine 0024 % 0025 0026 % ------ 0027 % Author: David Legland 0028 % E-mail: david.legland@inrae.fr 0029 % Created: 2003-10-31 0030 % Copyright 2003-2024 INRA - TPV URPOI - BIA IMASTE 0031 0032 isLine3d = @(x) validateattributes(x,{'numeric'},... 0033 {'nonempty','nonnan','real','finite','size',[nan,4]}); 0034 defOpts.Color = 'b'; 0035 [ax, lin, varargin] = ... 0036 parseDrawInput(lin, isLine3d, 'line', defOpts, varargin{:}); 0037 0038 % extract bounding box of the current axis 0039 xlim = get(ax, 'xlim'); 0040 ylim = get(ax, 'ylim'); 0041 0042 % clip lines with current axis box 0043 clip = clipLine(lin, [xlim ylim]); 0044 ok = isfinite(clip(:,1)); 0045 0046 % initialize result array to invalide handles 0047 h = -1 * ones(size(lin, 1), 1); 0048 0049 % draw valid lines 0050 h(ok) = plot(ax, clip(ok, [1 3])', clip(ok, [2 4])', varargin{:}); 0051 0052 % return line handle if needed 0053 if nargout > 0 0054 varargout = {h}; 0055 end