RANDOMPOINTINBOX Generate random point within a box. PTS = randomPointInBox(BOX) Generate a random point within the box BOX. The result is a 1-by-2 row vector. PTS = randomPointInBox(BOX, N) Generates N points within the box. The result is a N-by-2 array. BOX has the format: BOX = [xmin xmax ymin ymax]. Example % draw points within a box box = [10 80 20 60]; pts = randomPointInBox(box, 500); figure(1); clf; hold on; drawBox(box); drawPoint(pts, '.'); axis('equal'); axis([0 100 0 100]); See also geom2d, points2d, boxes2d, randomPointInBox3d, randomPointInPolygon
0001 function points = randomPointInBox(box, N, varargin) 0002 %RANDOMPOINTINBOX Generate random point within a box. 0003 % 0004 % PTS = randomPointInBox(BOX) 0005 % Generate a random point within the box BOX. The result is a 1-by-2 row 0006 % vector. 0007 % 0008 % PTS = randomPointInBox(BOX, N) 0009 % Generates N points within the box. The result is a N-by-2 array. 0010 % 0011 % BOX has the format: 0012 % BOX = [xmin xmax ymin ymax]. 0013 % 0014 % Example 0015 % % draw points within a box 0016 % box = [10 80 20 60]; 0017 % pts = randomPointInBox(box, 500); 0018 % figure(1); clf; hold on; 0019 % drawBox(box); 0020 % drawPoint(pts, '.'); 0021 % axis('equal'); 0022 % axis([0 100 0 100]); 0023 % 0024 % See also 0025 % geom2d, points2d, boxes2d, randomPointInBox3d, randomPointInPolygon 0026 % 0027 0028 % ------ 0029 % Author: David Legland 0030 % e-mail: david.legland@inra.fr 0031 % Created: 2007-10-10, using Matlab 7.4.0.287 (R2007a) 0032 % Copyright 2007 INRA - BIA PV Nantes - MIAJ Jouy-en-Josas. 0033 0034 if nargin < 2 0035 N = 1; 0036 end 0037 0038 % extract box bounds 0039 xmin = box(1); 0040 xmax = box(2); 0041 ymin = box(3); 0042 ymax = box(4); 0043 0044 % compute size of box 0045 dx = xmax - xmin; 0046 dy = ymax - ymin; 0047 0048 % compute point coordinates 0049 points = [rand(N, 1)*dx+xmin , rand(N, 1)*dy+ymin];