DRAWCENTEREDEDGE Draw an edge centered on a point. Draws a centered edge, defined by a center, a length and an orientation (in degrees). This function can be used to draw principal axes of an ellipse or of an oriented box. drawCenteredEdge(EDGE) Draws an edge centered on a point. EDGE has format [XC YC L THETA], with (Xc, YC) being edge center, L being the edge length, and THETA beigng the edge orientation, in degrees (counted Counter-clockwise from horizontal). Input argument can also be a N-by-4 array, in that can several edges are drawn. drawCenteredEdge(CENTER, L, THETA) Specifies argument in seperate inputs. drawCenteredEdge(..., NAME, VALUE) Also specifies drawing options by using one or several parameter name - value pairs (see doc of plot function for details). drawCenteredEdge(AX, ...) Specifies the axis to draw the edge on. H = drawCenteredEdge(...) Returns handle(s) to the created edges(s). Example % Draw an ellipse with its two axes figure(1); clf; center = [50 40]; r1 = 30; r2 = 10; theta = 20; elli = [center r1 r2 theta]; drawEllipse(elli, 'linewidth', 2); axis([0 100 0 100]); axis equal; hold on; edges = [center 2*r1 theta ; center 2*r2 theta+90]; drawCenteredEdge(edges, 'linewidth', 2, 'color', 'g'); See also edges2d, drawOrientedBox, drawEllipse, centeredEdgeToEdge, drawEdge
0001 function varargout = drawCenteredEdge(varargin) 0002 %DRAWCENTEREDEDGE Draw an edge centered on a point. 0003 % 0004 % Draws a centered edge, defined by a center, a length and an orientation 0005 % (in degrees). This function can be used to draw principal axes of an 0006 % ellipse or of an oriented box. 0007 % 0008 % 0009 % drawCenteredEdge(EDGE) 0010 % Draws an edge centered on a point. EDGE has format [XC YC L THETA], 0011 % with (Xc, YC) being edge center, L being the edge length, and THETA 0012 % beigng the edge orientation, in degrees (counted Counter-clockwise from 0013 % horizontal). 0014 % Input argument can also be a N-by-4 array, in that can several edges 0015 % are drawn. 0016 % 0017 % drawCenteredEdge(CENTER, L, THETA) 0018 % Specifies argument in seperate inputs. 0019 % 0020 % drawCenteredEdge(..., NAME, VALUE) 0021 % Also specifies drawing options by using one or several parameter name - 0022 % value pairs (see doc of plot function for details). 0023 % 0024 % drawCenteredEdge(AX, ...) 0025 % Specifies the axis to draw the edge on. 0026 % 0027 % H = drawCenteredEdge(...) 0028 % Returns handle(s) to the created edges(s). 0029 % 0030 % Example 0031 % % Draw an ellipse with its two axes 0032 % figure(1); clf; 0033 % center = [50 40]; 0034 % r1 = 30; r2 = 10; 0035 % theta = 20; 0036 % elli = [center r1 r2 theta]; 0037 % drawEllipse(elli, 'linewidth', 2); 0038 % axis([0 100 0 100]); axis equal; 0039 % hold on; 0040 % edges = [center 2*r1 theta ; center 2*r2 theta+90]; 0041 % drawCenteredEdge(edges, 'linewidth', 2, 'color', 'g'); 0042 % 0043 % See also 0044 % edges2d, drawOrientedBox, drawEllipse, centeredEdgeToEdge, drawEdge 0045 % 0046 0047 % ------ 0048 % Author: David Legland 0049 % E-mail: david.legland@inrae.fr 0050 % Created: 2005-08-05 0051 % Copyright 2005-2024 INRA - TPV URPOI - BIA IMASTE 0052 0053 %% process input variables 0054 0055 if nargin < 1 0056 error('Function requires an input argument'); 0057 end 0058 0059 % extract handle of axis to draw on 0060 if isAxisHandle(varargin{1}) 0061 ax = varargin{1}; 0062 varargin(1) = []; 0063 else 0064 ax = gca; 0065 end 0066 0067 var = varargin{1}; 0068 if size(var, 2) == 4 0069 % manage edge in single parameter 0070 len = var(:, 3); 0071 theta = var(:, 4); 0072 center = var(:, 1:2); 0073 0074 N = size(center, 1); 0075 varargin(1) = []; 0076 0077 elseif length(varargin) >= 3 0078 % parameters given in different arguments 0079 0080 % size of data 0081 center = varargin{1}; 0082 len = varargin{2}; 0083 theta = varargin{3}; 0084 varargin(1:3) = []; 0085 0086 % ensure all data have same size 0087 NP = size(center, 1); 0088 NL = size(len, 1); 0089 ND = size(theta, 1); 0090 N = max([NP NL ND]); 0091 if N > 1 0092 if NP == 1, center = repmat(center, [N 1]); end 0093 if NL == 1, len = repmat(len, [N 1]); end 0094 if ND == 1, theta = repmat(theta, [N 1]); end 0095 end 0096 0097 end 0098 0099 % extract drawing options 0100 options = varargin(:); 0101 0102 0103 %% Draw edges 0104 0105 % coordinates of center point 0106 xc = center(:, 1); 0107 yc = center(:, 2); 0108 0109 % convert angle to radians 0110 theta = theta * pi / 180; 0111 0112 % computation shortcuts 0113 cot = cos(theta); 0114 sit = sin(theta); 0115 0116 % compute starting and ending points 0117 x1 = xc - len .* cot / 2; 0118 x2 = xc + len .* cot / 2; 0119 y1 = yc - len .* sit / 2; 0120 y2 = yc + len .* sit / 2; 0121 0122 % save hold state 0123 holdState = ishold(ax); 0124 hold(ax, 'on'); 0125 0126 0127 % draw the edges 0128 h = zeros(N, 1); 0129 for i = 1:N 0130 h(i) = plot(ax, [x1(i) x2(i)], [y1(i) y2(i)]); 0131 end 0132 0133 % apply style to edges 0134 if ~isempty(options) > 0 0135 for i = 1:N 0136 set(h(i), options{:}); 0137 end 0138 end 0139 0140 0141 %% Format output 0142 0143 % restore hold state 0144 if ~holdState 0145 hold(ax, 'off'); 0146 end 0147 0148 % process output arguments 0149 if nargout > 0 0150 varargout = {h}; 0151 end