Computes the bounding box of a polygon. BOX = polygonBounds(POLY); Returns the bounding box of the polygon. BOX has the format: [XMIN XMAX YMIN YMAX]. Input polygon POLY is as a N-by-2 array containing coordinates of each vertex. Multiple polygons can be specified either by inserting NaN rows between vertex coordinates, or by using a cell array, each cell containing the vertex coordinates of a polygon loop. See also polygons2d, boxes2d, boundingBox
0001 function box = polygonBounds(polygon) 0002 % Computes the bounding box of a polygon. 0003 % 0004 % BOX = polygonBounds(POLY); 0005 % Returns the bounding box of the polygon. 0006 % BOX has the format: [XMIN XMAX YMIN YMAX]. 0007 % 0008 % Input polygon POLY is as a N-by-2 array containing coordinates of each 0009 % vertex. 0010 % Multiple polygons can be specified either by inserting NaN rows between 0011 % vertex coordinates, or by using a cell array, each cell containing the 0012 % vertex coordinates of a polygon loop. 0013 % 0014 % See also 0015 % polygons2d, boxes2d, boundingBox 0016 % 0017 0018 % ------ 0019 % Author: David Legland 0020 % e-mail: david.legland@inrae.fr 0021 % Created: 2007-10-12, using Matlab 7.4.0.287 (R2007a) 0022 % Copyright 2007 INRA - Cepia software platform 0023 0024 0025 % transform as a cell array of simple polygons 0026 polygons = splitPolygons(polygon); 0027 0028 % init extreme values 0029 xmin = inf; 0030 xmax = -inf; 0031 ymin = inf; 0032 ymax = -inf; 0033 0034 % iterate over loops 0035 for i = 1:length(polygons) 0036 polygon = polygons{i}; 0037 0038 xmin = min(xmin, min(polygon(:,1))); 0039 xmax = max(xmax, max(polygon(:,1))); 0040 ymin = min(ymin, min(polygon(:,2))); 0041 ymax = max(ymax, max(polygon(:,2))); 0042 end 0043 0044 % format output 0045 box = [xmin xmax ymin ymax];