DefineZone - Define a restricted zone to which analyses can be circumscribed. Define a restricted zone in the environment, to which analyses can subsequently be circumscribed using <a href="matlab:help IsInZone">IsInZone</a>. Multiple zones can easily be combined (see example). USAGE zone = DefineZone(size,shape,points) size environment size [h v] shape 'rectangle' or 'circle' points list of points defining the field rectangle: [x y width height] circle: [x y radius] EXAMPLE % Define a rectangular and a circular zones, and combine them r = DefineZone([100 100],'rectangle',[20 30 50 20]); c = DefineZone([100 100],'circle',[50 70 15]); combined = c|r; SEE See also FiringMap, IsInZone.
0001 function zone = DefineZone(s,shape,points) 0002 0003 %DefineZone - Define a restricted zone to which analyses can be circumscribed. 0004 % 0005 % Define a restricted zone in the environment, to which analyses can subsequently be 0006 % circumscribed using <a href="matlab:help IsInZone">IsInZone</a>. Multiple zones can easily be combined (see example). 0007 % 0008 % USAGE 0009 % 0010 % zone = DefineZone(size,shape,points) 0011 % 0012 % size environment size [h v] 0013 % shape 'rectangle' or 'circle' 0014 % points list of points defining the field 0015 % rectangle: [x y width height] 0016 % circle: [x y radius] 0017 % 0018 % EXAMPLE 0019 % 0020 % % Define a rectangular and a circular zones, and combine them 0021 % r = DefineZone([100 100],'rectangle',[20 30 50 20]); 0022 % c = DefineZone([100 100],'circle',[50 70 15]); 0023 % combined = c|r; 0024 % 0025 % SEE 0026 % 0027 % See also FiringMap, IsInZone. 0028 0029 % Copyright (C) 2004-2011 by Michaƫl Zugaro 0030 % 0031 % This program is free software; you can redistribute it and/or modify 0032 % it under the terms of the GNU General Public License as published by 0033 % the Free Software Foundation; either version 3 of the License, or 0034 % (at your option) any later version. 0035 0036 if nargin < 3, 0037 error('Incorrect number of parameters (type ''help <a href="matlab:help DefineZone">DefineZone</a>'' for details).'); 0038 end 0039 if ~isdvector(s,'#2','>0'), 0040 error('Incorrect size (type ''help <a href="matlab:help DefineZone">DefineZone</a>'' for details).'); 0041 end 0042 if ~isastring(shape,'rectangle','circle'), 0043 error('Incorrect shape (type ''help <a href="matlab:help DefineZone">DefineZone</a>'' for details).'); 0044 end 0045 points = round(points); 0046 0047 width = s(1); 0048 height = s(2); 0049 0050 zone = logical(zeros(height,width)); 0051 0052 if strcmp(lower(shape),'rectangle'), 0053 if length(points) ~= 4, 0054 error('Incorrect points (type ''help <a href="matlab:help DefineZone">DefineZone</a>'' for details).'); 0055 end 0056 x = points(1); 0057 y = points(2); 0058 w = points(3); 0059 h = points(4); 0060 zone(y:y+h-1,x:x+w-1) = 1; 0061 elseif strcmp(lower(shape),'circle'), 0062 if length(points) ~= 3, 0063 error('Incorrect points (type ''help <a href="matlab:help DefineZone">DefineZone</a>'' for details).'); 0064 end 0065 x = points(1); 0066 y = points(2); 0067 r = points(3); 0068 for i = 1:width, 0069 for j = 1:height, 0070 if (x-i)^2+(y-j)^2 <= r^2, 0071 zone(j,i) = 1; 0072 end 0073 end 0074 end 0075 end 0076 0077 % Make sure the environment does not increase in size if the zone exceeds its boundaries 0078 zone = zone(1:height,1:width);