BOUNDINGBOX Bounding box of a set of points. BOX = boundingBox(POINTS) Returns the bounding box of the set of points POINTS. POINTS can be either a N-by-2 or N-by-3 array. The result BOX is a 1-by-4 or 1-by-6 array, containing: [XMIN XMAX YMIN YMAX] (2D point sets) [XMIN XMAX YMIN YMAX ZMIN ZMAX] (3D point sets) Example % Draw the bounding box of a set of random points points = rand(30, 2); figure; hold on; drawPoint(points, '.'); box = boundingBox(points); drawBox(box, 'r'); % Draw bounding box of a cubeoctehedron [v e f] = createCubeOctahedron; box3d = boundingBox(v); figure; hold on; drawMesh(v, f); drawBox3d(box3d); set(gcf, 'renderer', 'opengl') axis([-2 2 -2 2 -2 2]); view(3) See also polygonBounds, drawBox
0001 function box = boundingBox(points) 0002 %BOUNDINGBOX Bounding box of a set of points. 0003 % 0004 % BOX = boundingBox(POINTS) 0005 % Returns the bounding box of the set of points POINTS. POINTS can be 0006 % either a N-by-2 or N-by-3 array. The result BOX is a 1-by-4 or 1-by-6 0007 % array, containing: 0008 % [XMIN XMAX YMIN YMAX] (2D point sets) 0009 % [XMIN XMAX YMIN YMAX ZMIN ZMAX] (3D point sets) 0010 % 0011 % Example 0012 % % Draw the bounding box of a set of random points 0013 % points = rand(30, 2); 0014 % figure; hold on; 0015 % drawPoint(points, '.'); 0016 % box = boundingBox(points); 0017 % drawBox(box, 'r'); 0018 % 0019 % % Draw bounding box of a cubeoctehedron 0020 % [v e f] = createCubeOctahedron; 0021 % box3d = boundingBox(v); 0022 % figure; hold on; 0023 % drawMesh(v, f); 0024 % drawBox3d(box3d); 0025 % set(gcf, 'renderer', 'opengl') 0026 % axis([-2 2 -2 2 -2 2]); 0027 % view(3) 0028 % 0029 % See also 0030 % polygonBounds, drawBox 0031 % 0032 0033 % ------ 0034 % Author: David Legland 0035 % E-mail: david.legland@inrae.fr 0036 % Created: 2011-04-01, using Matlab 7.9.0.529 (R2009b) 0037 % Copyright 2011-2024 INRA - Cepia Software Platform 0038 0039 % compute extreme x and y values 0040 xmin = min(points(:,1)); 0041 xmax = max(points(:,1)); 0042 ymin = min(points(:,2)); 0043 ymax = max(points(:,2)); 0044 0045 if size(points, 2) > 2 0046 % process case of 3D points 0047 zmin = min(points(:,3)); 0048 zmax = max(points(:,3)); 0049 0050 % format as box 3D data structure 0051 box = [xmin xmax ymin ymax zmin zmax]; 0052 else 0053 % format as box data structure 0054 box = [xmin xmax ymin ymax]; 0055 end