Home > matGeom > geom2d > drawOrientedBox.m

drawOrientedBox

PURPOSE ^

DRAWORIENTEDBOX Draw centered oriented rectangle.

SYNOPSIS ^

function varargout = drawOrientedBox(varargin)

DESCRIPTION ^

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = drawOrientedBox(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 % ------
0041 % Author: David Legland
0042 % E-mail: david.legland@inrae.fr
0043 % Created: 2011-05-09, using Matlab 7.9.0.529 (R2009b)
0044 % Copyright 2011-2024 INRA - Cepia Software Platform
0045 
0046 %% Parses input arguments
0047 
0048 % extract handle of axis to draw on
0049 [ax, varargin] = parseAxisHandle(varargin{:});
0050 
0051 % extract shape primitive
0052 box = varargin{1};
0053 varargin(1) = [];
0054 
0055 if length(varargin) > 4 && sum(cellfun(@isnumeric, varargin(1:4))) == 4
0056     % input given as separate arguments
0057     cx  = box;
0058     cy  = varargin{1};
0059     hl   = varargin{2} / 2;
0060     hw   = varargin{3} / 2;
0061     theta   = varargin{4};
0062     varargin = varargin(5:end);
0063     
0064 else
0065     % input given as packed array
0066     cx  = box(:,1);
0067     cy  = box(:,2);
0068     hl   = box(:,3) / 2;
0069     hw   = box(:,4) / 2;
0070     theta = box(:,5);
0071 end
0072 
0073 
0074 %% Pre-processing
0075 
0076 % allocate memory for graphical handle
0077 hr = zeros(length(cx), 1);
0078 
0079 % save hold state
0080 holdState = ishold(ax);
0081 hold(ax, 'on');
0082 
0083 
0084 %% Draw each box
0085 
0086 % iterate on oriented boxes
0087 for i = 1:length(cx)
0088     % pre-compute angle data
0089     cot = cosd(theta(i));
0090     sit = sind(theta(i));
0091     
0092     % x and y shifts
0093     lc = hl(i) * cot;
0094     ls = hl(i) * sit;
0095     wc = hw(i) * cot;
0096     ws = hw(i) * sit;
0097 
0098     % coordinates of box vertices
0099     vx = cx(i) + [-lc + ws; lc + ws ; lc - ws ; -lc - ws ; -lc + ws];
0100     vy = cy(i) + [-ls - wc; ls - wc ; ls + wc ; -ls + wc ; -ls - wc];
0101 
0102     % draw polygons
0103     hr(i) = plot(ax, vx, vy, varargin{:});
0104 end
0105 
0106 
0107 %% Post-processing
0108 
0109 % restore hold state
0110 if ~holdState
0111     hold(ax, 'off');
0112 end
0113 
0114 % Format output
0115 if nargout > 0
0116     varargout = {hr};
0117 end

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022