DRAWORIENTEDBOX Draw centered oriented rectangle. Syntax drawOrientedBox(BOX) drawOrientedBox(BOX, 'PropertyName', propertyvalue, ...) Description drawOrientedBox(OBOX) Draws an oriented rectangle (or bounding box) on the current axis. OBOX is a 1-by-5 row vector containing box center, dimension (length and width) and orientation (in degrees): OBOX = [CX CY LENGTH WIDTH THETA]. When OBOX is a N-by-5 array, the N boxes are drawn. drawOrientedBox(AX, ...) Specifies the axis to draw to point in. AX should be a handle to a axis object. By default, display on current axis. HB = drawOrientedBox(...) Returns a handle to the created graphic object(s). Object style can be modified using syntaw like: set(HB, 'color', 'g', 'linewidth', 2); Example % draw an ellipse together with its oriented box elli = [30 40 60 30 20]; figure; drawEllipse(elli, 'linewidth', 2, 'color', 'g'); hold on box = [30 40 120 60 20]; drawOrientedBox(box, 'color', 'k'); axis equal; See also orientedBox, drawPolygon, drawRect, drawBox, drawCenteredEdge ------ Author: David Legland e-mail: david.legland@grignon.inra.fr Created: 2011-05-09, using Matlab 7.9.0.529 (R2009b) Copyright 2011 INRA - Cepia Software Platform.
0001 function varargout = drawOrientedBox(box, varargin) 0002 %DRAWORIENTEDBOX Draw centered oriented rectangle. 0003 % 0004 % Syntax 0005 % drawOrientedBox(BOX) 0006 % drawOrientedBox(BOX, 'PropertyName', propertyvalue, ...) 0007 % 0008 % Description 0009 % drawOrientedBox(OBOX) 0010 % Draws an oriented rectangle (or bounding box) on the current axis. 0011 % OBOX is a 1-by-5 row vector containing box center, dimension (length 0012 % and width) and orientation (in degrees): 0013 % OBOX = [CX CY LENGTH WIDTH THETA]. 0014 % 0015 % When OBOX is a N-by-5 array, the N boxes are drawn. 0016 % 0017 % drawOrientedBox(AX, ...) 0018 % Specifies the axis to draw to point in. AX should be a handle to a axis 0019 % object. By default, display on current axis. 0020 % 0021 % HB = drawOrientedBox(...) 0022 % Returns a handle to the created graphic object(s). Object style can be 0023 % modified using syntaw like: 0024 % set(HB, 'color', 'g', 'linewidth', 2); 0025 % 0026 % Example 0027 % % draw an ellipse together with its oriented box 0028 % elli = [30 40 60 30 20]; 0029 % figure; 0030 % drawEllipse(elli, 'linewidth', 2, 'color', 'g'); 0031 % hold on 0032 % box = [30 40 120 60 20]; 0033 % drawOrientedBox(box, 'color', 'k'); 0034 % axis equal; 0035 % 0036 % See also 0037 % orientedBox, drawPolygon, drawRect, drawBox, drawCenteredEdge 0038 % 0039 % ------ 0040 % Author: David Legland 0041 % e-mail: david.legland@grignon.inra.fr 0042 % Created: 2011-05-09, using Matlab 7.9.0.529 (R2009b) 0043 % Copyright 2011 INRA - Cepia Software Platform. 0044 0045 % HISTORY 0046 % 2011-07-22 simplifies code 0047 % 2011-10-11 add management of axes handle 0048 0049 0050 %% Parses input arguments 0051 0052 % extract handle of axis to draw on 0053 if isAxisHandle(box) 0054 ax = box; 0055 box = varargin{1}; 0056 varargin(1) = []; 0057 else 0058 ax = gca; 0059 end 0060 0061 if length(varargin) > 4 && sum(cellfun(@isnumeric, varargin(1:4))) == 4 0062 % input given as separate arguments 0063 cx = box; 0064 cy = varargin{1}; 0065 hl = varargin{2} / 2; 0066 hw = varargin{3} / 2; 0067 theta = varargin{4}; 0068 varargin = varargin(5:end); 0069 0070 else 0071 % input given as packed array 0072 cx = box(:,1); 0073 cy = box(:,2); 0074 hl = box(:,3) / 2; 0075 hw = box(:,4) / 2; 0076 theta = box(:,5); 0077 end 0078 0079 0080 %% Draw each box 0081 0082 % allocate memory for graphical handle 0083 hr = zeros(length(cx), 1); 0084 0085 % iterate on oriented boxes 0086 for i = 1:length(cx) 0087 % pre-compute angle data 0088 cot = cosd(theta(i)); 0089 sit = sind(theta(i)); 0090 0091 % x and y shifts 0092 lc = hl(i) * cot; 0093 ls = hl(i) * sit; 0094 wc = hw(i) * cot; 0095 ws = hw(i) * sit; 0096 0097 % coordinates of box vertices 0098 vx = cx(i) + [-lc + ws; lc + ws ; lc - ws ; -lc - ws ; -lc + ws]; 0099 vy = cy(i) + [-ls - wc; ls - wc ; ls + wc ; -ls + wc ; -ls - wc]; 0100 0101 % draw polygons 0102 hr(i) = plot(ax, vx, vy, varargin{:}); 0103 end 0104 0105 0106 %% Format output 0107 0108 if nargout > 0 0109 varargout = {hr}; 0110 end