Home > matGeom > geom2d > rectToPolygon.m

rectToPolygon

PURPOSE ^

RECTTOPOLYGON Convert a rectangle into a polygon (set of vertices).

SYNOPSIS ^

function [tx, ty] = rectToPolygon(rect)

DESCRIPTION ^

RECTTOPOLYGON Convert a rectangle into a polygon (set of vertices).

   POLY = rectToPolygon(RECT);
   Converts rectangle given as [X0 Y0 W H] or [X0 Y0 W H THETA] into a
   4-by-2 array double, containing coordinate of rectangle vertices.
   X0 and Y0 are the coordinates of the "lower left" vertex (before
   applying rotation), W and H are the width and the height of the
   rectangle, and THETA is the rotation angle around the first vertex, in
   degrees.

   See also:
   orientedBoxToPolygon, ellipseToPolygon, drawRect, drawPolygon

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [tx, ty] = rectToPolygon(rect)
0002 %RECTTOPOLYGON Convert a rectangle into a polygon (set of vertices).
0003 %
0004 %   POLY = rectToPolygon(RECT);
0005 %   Converts rectangle given as [X0 Y0 W H] or [X0 Y0 W H THETA] into a
0006 %   4-by-2 array double, containing coordinate of rectangle vertices.
0007 %   X0 and Y0 are the coordinates of the "lower left" vertex (before
0008 %   applying rotation), W and H are the width and the height of the
0009 %   rectangle, and THETA is the rotation angle around the first vertex, in
0010 %   degrees.
0011 %
0012 %   See also:
0013 %   orientedBoxToPolygon, ellipseToPolygon, drawRect, drawPolygon
0014 %
0015 %
0016 
0017 % ---------
0018 % Author: David Legland
0019 % e-mail: david.legland@nantes.inra.fr
0020 % INRA - TPV URPOI - BIA IMASTE
0021 % created the 06/04/2005.
0022 %
0023 
0024 % HISTORY
0025 
0026 % extract rectangle parameters
0027 theta = 0;
0028 x0  = rect(1);
0029 y0  = rect(2);
0030 w   = rect(3);
0031 h   = rect(4);
0032 if length(rect) > 4
0033     theta = rect(5);
0034 end
0035 
0036 % precompute angular quantities
0037 cot = cosd(theta);
0038 sit = sind(theta);
0039 
0040 % compute vertex coordinates
0041 tx = zeros(4, 1);
0042 ty = zeros(4, 1);
0043 tx(1) = x0;
0044 ty(1) = y0;
0045 tx(2) = x0 + w * cot;
0046 ty(2) = y0 + w * sit;
0047 tx(3) = x0 + w * cot - h * sit;
0048 ty(3) = y0 + w * sit + h * cot;
0049 tx(4) = x0 - h * sit;
0050 ty(4) = y0 + h * cot;
0051 
0052 % format output
0053 if nargout <= 1
0054     tx = [tx ty];
0055 end

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019