ORIENTEDBOXTOPOLYGON Convert an oriented box to a polygon (set of vertices). POLY = orientedBoxToPolygon(OBOX); Converts the oriented box OBOX given either as [XC YC W H] or as [XC YC W H THETA] into a 4-by-2 array of double, containing coordinates of box vertices. XC and YC are center of the box. W and H are the width and the height (dimension in the main directions), and THETA is the orientation, in degrees between 0 and 360. Example OBOX = [20 10 40 20 0]; RECT = orientedBoxToPolygon(OBOX) RECT = -20 -10 20 -10 20 10 -20 10 See also: polygons2d, orientedBox, drawOrientedBox, rectToPolygon
0001 function [tx, ty] = orientedBoxToPolygon(obox) 0002 %ORIENTEDBOXTOPOLYGON Convert an oriented box to a polygon (set of vertices). 0003 % 0004 % POLY = orientedBoxToPolygon(OBOX); 0005 % Converts the oriented box OBOX given either as [XC YC W H] or as 0006 % [XC YC W H THETA] into a 4-by-2 array of double, containing coordinates 0007 % of box vertices. 0008 % XC and YC are center of the box. W and H are the width and the height 0009 % (dimension in the main directions), and THETA is the orientation, in 0010 % degrees between 0 and 360. 0011 % 0012 % Example 0013 % OBOX = [20 10 40 20 0]; 0014 % RECT = orientedBoxToPolygon(OBOX) 0015 % RECT = 0016 % -20 -10 0017 % 20 -10 0018 % 20 10 0019 % -20 10 0020 % 0021 % 0022 % See also: 0023 % polygons2d, orientedBox, drawOrientedBox, rectToPolygon 0024 % 0025 0026 % --------- 0027 % Author: David Legland 0028 % e-mail: david.legland@nantes.inra.fr 0029 % INRA - TPV URPOI - BIA IMASTE 0030 % created the 06/04/2005. 0031 % 0032 0033 % HISTORY 0034 % 2011-10-09 rewrite from rectAsPolygon to orientedBoxToPolygon 0035 % 2016: Simplify by JuanPi Carbajal 0036 0037 % extract box parameters 0038 theta = 0; 0039 x = obox(1); 0040 y = obox(2); 0041 w = obox(3) / 2; % easier to compute with w and h divided by 2 0042 h = obox(4) / 2; 0043 if length(obox) > 4 0044 theta = obox(5); 0045 end 0046 0047 v = [cosd(theta); sind(theta)]; 0048 M = bsxfun (@times, [-1 1; 1 1; 1 -1; -1 -1], [w h]); 0049 tx = x + M * v; 0050 ty = y + M(4:-1:1,[2 1]) * v; 0051 0052 if nargout <= 1 0053 tx = [tx ty]; 0054 end